{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Riskfolio-Lib Tutorial: \n",
    "<br>__[Financionerioncios](https://financioneroncios.wordpress.com)__\n",
    "<br>__[Orenji](https://www.orenj-i.net)__\n",
    "<br>__[Riskfolio-Lib](https://riskfolio-lib.readthedocs.io/en/latest/)__\n",
    "<br>__[Dany Cajas](https://www.linkedin.com/in/dany-cajas/)__\n",
    "<a href='https://ko-fi.com/B0B833SXD' target='_blank'><img height='36' style='border:0px;height:36px;' src='https://cdn.ko-fi.com/cdn/kofi1.png?v=2' border='0' alt='Buy Me a Coffee at ko-fi.com' /></a> \n",
    "\n",
    "## Tutorial 4: Bond Portfolio Optimization and Immunization\n",
    "\n",
    "If you want to know more about the mathematics behind this model you can check the following posts:  __[Valorización de Bonos con Python parte II](https://financioneroncios.wordpress.com/2018/05/23/valorizacion-de-bonos-con-python-parte-ii/)__, __[Fixed Income Portfolio Optimization with Python](https://financioneroncios.wordpress.com/2020/01/09/fixed-income-portfolio-optimization-with-python/)__ \n",
    "\n",
    "## 1. Uploading the data:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style  type=\"text/css\" >\n",
       "</style><table id=\"T_eb8e6_\" ><thead>    <tr>        <th class=\"blank level0\" ></th>        <th class=\"col_heading level0 col0\" >0</th>        <th class=\"col_heading level0 col1\" >90</th>        <th class=\"col_heading level0 col2\" >180</th>        <th class=\"col_heading level0 col3\" >360</th>        <th class=\"col_heading level0 col4\" >720</th>        <th class=\"col_heading level0 col5\" >1800</th>        <th class=\"col_heading level0 col6\" >3600</th>        <th class=\"col_heading level0 col7\" >7200</th>        <th class=\"col_heading level0 col8\" >10800</th>    </tr>    <tr>        <th class=\"index_name level0\" >Date</th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>    </tr></thead><tbody>\n",
       "                <tr>\n",
       "                        <th id=\"T_eb8e6_level0_row0\" class=\"row_heading level0 row0\" >2017-11-16 00:00:00</th>\n",
       "                        <td id=\"T_eb8e6_row0_col0\" class=\"data row0 col0\" >0.0000%</td>\n",
       "                        <td id=\"T_eb8e6_row0_col1\" class=\"data row0 col1\" >0.0059%</td>\n",
       "                        <td id=\"T_eb8e6_row0_col2\" class=\"data row0 col2\" >0.0108%</td>\n",
       "                        <td id=\"T_eb8e6_row0_col3\" class=\"data row0 col3\" >0.0178%</td>\n",
       "                        <td id=\"T_eb8e6_row0_col4\" class=\"data row0 col4\" >0.0246%</td>\n",
       "                        <td id=\"T_eb8e6_row0_col5\" class=\"data row0 col5\" >0.0213%</td>\n",
       "                        <td id=\"T_eb8e6_row0_col6\" class=\"data row0 col6\" >0.0075%</td>\n",
       "                        <td id=\"T_eb8e6_row0_col7\" class=\"data row0 col7\" >-0.0048%</td>\n",
       "                        <td id=\"T_eb8e6_row0_col8\" class=\"data row0 col8\" >-0.0093%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_eb8e6_level0_row1\" class=\"row_heading level0 row1\" >2017-11-15 00:00:00</th>\n",
       "                        <td id=\"T_eb8e6_row1_col0\" class=\"data row1 col0\" >0.0180%</td>\n",
       "                        <td id=\"T_eb8e6_row1_col1\" class=\"data row1 col1\" >0.0247%</td>\n",
       "                        <td id=\"T_eb8e6_row1_col2\" class=\"data row1 col2\" >0.0303%</td>\n",
       "                        <td id=\"T_eb8e6_row1_col3\" class=\"data row1 col3\" >0.0391%</td>\n",
       "                        <td id=\"T_eb8e6_row1_col4\" class=\"data row1 col4\" >0.0495%</td>\n",
       "                        <td id=\"T_eb8e6_row1_col5\" class=\"data row1 col5\" >0.0558%</td>\n",
       "                        <td id=\"T_eb8e6_row1_col6\" class=\"data row1 col6\" >0.0512%</td>\n",
       "                        <td id=\"T_eb8e6_row1_col7\" class=\"data row1 col7\" >0.0450%</td>\n",
       "                        <td id=\"T_eb8e6_row1_col8\" class=\"data row1 col8\" >0.0417%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_eb8e6_level0_row2\" class=\"row_heading level0 row2\" >2017-11-14 00:00:00</th>\n",
       "                        <td id=\"T_eb8e6_row2_col0\" class=\"data row2 col0\" >-0.1800%</td>\n",
       "                        <td id=\"T_eb8e6_row2_col1\" class=\"data row2 col1\" >-0.1710%</td>\n",
       "                        <td id=\"T_eb8e6_row2_col2\" class=\"data row2 col2\" >-0.1624%</td>\n",
       "                        <td id=\"T_eb8e6_row2_col3\" class=\"data row2 col3\" >-0.1460%</td>\n",
       "                        <td id=\"T_eb8e6_row2_col4\" class=\"data row2 col4\" >-0.1167%</td>\n",
       "                        <td id=\"T_eb8e6_row2_col5\" class=\"data row2 col5\" >-0.0506%</td>\n",
       "                        <td id=\"T_eb8e6_row2_col6\" class=\"data row2 col6\" >0.0140%</td>\n",
       "                        <td id=\"T_eb8e6_row2_col7\" class=\"data row2 col7\" >0.0676%</td>\n",
       "                        <td id=\"T_eb8e6_row2_col8\" class=\"data row2 col8\" >0.0861%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_eb8e6_level0_row3\" class=\"row_heading level0 row3\" >2017-11-13 00:00:00</th>\n",
       "                        <td id=\"T_eb8e6_row3_col0\" class=\"data row3 col0\" >0.0000%</td>\n",
       "                        <td id=\"T_eb8e6_row3_col1\" class=\"data row3 col1\" >0.0013%</td>\n",
       "                        <td id=\"T_eb8e6_row3_col2\" class=\"data row3 col2\" >0.0025%</td>\n",
       "                        <td id=\"T_eb8e6_row3_col3\" class=\"data row3 col3\" >0.0048%</td>\n",
       "                        <td id=\"T_eb8e6_row3_col4\" class=\"data row3 col4\" >0.0088%</td>\n",
       "                        <td id=\"T_eb8e6_row3_col5\" class=\"data row3 col5\" >0.0174%</td>\n",
       "                        <td id=\"T_eb8e6_row3_col6\" class=\"data row3 col6\" >0.0258%</td>\n",
       "                        <td id=\"T_eb8e6_row3_col7\" class=\"data row3 col7\" >0.0334%</td>\n",
       "                        <td id=\"T_eb8e6_row3_col8\" class=\"data row3 col8\" >0.0364%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_eb8e6_level0_row4\" class=\"row_heading level0 row4\" >2017-11-10 00:00:00</th>\n",
       "                        <td id=\"T_eb8e6_row4_col0\" class=\"data row4 col0\" >0.0000%</td>\n",
       "                        <td id=\"T_eb8e6_row4_col1\" class=\"data row4 col1\" >0.0026%</td>\n",
       "                        <td id=\"T_eb8e6_row4_col2\" class=\"data row4 col2\" >0.0043%</td>\n",
       "                        <td id=\"T_eb8e6_row4_col3\" class=\"data row4 col3\" >0.0054%</td>\n",
       "                        <td id=\"T_eb8e6_row4_col4\" class=\"data row4 col4\" >0.0017%</td>\n",
       "                        <td id=\"T_eb8e6_row4_col5\" class=\"data row4 col5\" >-0.0248%</td>\n",
       "                        <td id=\"T_eb8e6_row4_col6\" class=\"data row4 col6\" >-0.0615%</td>\n",
       "                        <td id=\"T_eb8e6_row4_col7\" class=\"data row4 col7\" >-0.0936%</td>\n",
       "                        <td id=\"T_eb8e6_row4_col8\" class=\"data row4 col8\" >-0.1054%</td>\n",
       "            </tr>\n",
       "    </tbody></table>"
      ],
      "text/plain": [
       "<pandas.io.formats.style.Styler at 0x7ff27f5b06a0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<style  type=\"text/css\" >\n",
       "</style><table id=\"T_73060_\" ><thead>    <tr>        <th class=\"blank level0\" ></th>        <th class=\"col_heading level0 col0\" >APA</th>        <th class=\"col_heading level0 col1\" >CMCSA</th>        <th class=\"col_heading level0 col2\" >CNP</th>        <th class=\"col_heading level0 col3\" >HPQ</th>        <th class=\"col_heading level0 col4\" >PSA</th>        <th class=\"col_heading level0 col5\" >SEE</th>        <th class=\"col_heading level0 col6\" >ZION</th>        <th class=\"col_heading level0 col7\" >PEP11900D031</th>        <th class=\"col_heading level0 col8\" >PEP13000D012</th>        <th class=\"col_heading level0 col9\" >PEP13000M088</th>        <th class=\"col_heading level0 col10\" >PEP23900M103</th>        <th class=\"col_heading level0 col11\" >PEP70101M530</th>        <th class=\"col_heading level0 col12\" >PEP70101M571</th>        <th class=\"col_heading level0 col13\" >PEP70310M156</th>    </tr>    <tr>        <th class=\"index_name level0\" >Date</th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>    </tr></thead><tbody>\n",
       "                <tr>\n",
       "                        <th id=\"T_73060_level0_row0\" class=\"row_heading level0 row0\" >2017-11-16 00:00:00</th>\n",
       "                        <td id=\"T_73060_row0_col0\" class=\"data row0 col0\" >-1.3161%</td>\n",
       "                        <td id=\"T_73060_row0_col1\" class=\"data row0 col1\" >-0.2958%</td>\n",
       "                        <td id=\"T_73060_row0_col2\" class=\"data row0 col2\" >-1.0903%</td>\n",
       "                        <td id=\"T_73060_row0_col3\" class=\"data row0 col3\" >0.9831%</td>\n",
       "                        <td id=\"T_73060_row0_col4\" class=\"data row0 col4\" >1.7234%</td>\n",
       "                        <td id=\"T_73060_row0_col5\" class=\"data row0 col5\" >1.4016%</td>\n",
       "                        <td id=\"T_73060_row0_col6\" class=\"data row0 col6\" >-0.8387%</td>\n",
       "                        <td id=\"T_73060_row0_col7\" class=\"data row0 col7\" >-0.0411%</td>\n",
       "                        <td id=\"T_73060_row0_col8\" class=\"data row0 col8\" >-0.0380%</td>\n",
       "                        <td id=\"T_73060_row0_col9\" class=\"data row0 col9\" >-0.0597%</td>\n",
       "                        <td id=\"T_73060_row0_col10\" class=\"data row0 col10\" >-0.0737%</td>\n",
       "                        <td id=\"T_73060_row0_col11\" class=\"data row0 col11\" >-0.0116%</td>\n",
       "                        <td id=\"T_73060_row0_col12\" class=\"data row0 col12\" >0.0076%</td>\n",
       "                        <td id=\"T_73060_row0_col13\" class=\"data row0 col13\" >-0.0633%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_73060_level0_row1\" class=\"row_heading level0 row1\" >2017-11-15 00:00:00</th>\n",
       "                        <td id=\"T_73060_row1_col0\" class=\"data row1 col0\" >-2.0296%</td>\n",
       "                        <td id=\"T_73060_row1_col1\" class=\"data row1 col1\" >0.8682%</td>\n",
       "                        <td id=\"T_73060_row1_col2\" class=\"data row1 col2\" >-1.1202%</td>\n",
       "                        <td id=\"T_73060_row1_col3\" class=\"data row1 col3\" >0.0000%</td>\n",
       "                        <td id=\"T_73060_row1_col4\" class=\"data row1 col4\" >-1.3479%</td>\n",
       "                        <td id=\"T_73060_row1_col5\" class=\"data row1 col5\" >-0.3326%</td>\n",
       "                        <td id=\"T_73060_row1_col6\" class=\"data row1 col6\" >0.0215%</td>\n",
       "                        <td id=\"T_73060_row1_col7\" class=\"data row1 col7\" >-0.1626%</td>\n",
       "                        <td id=\"T_73060_row1_col8\" class=\"data row1 col8\" >-0.3076%</td>\n",
       "                        <td id=\"T_73060_row1_col9\" class=\"data row1 col9\" >-0.3041%</td>\n",
       "                        <td id=\"T_73060_row1_col10\" class=\"data row1 col10\" >-0.2286%</td>\n",
       "                        <td id=\"T_73060_row1_col11\" class=\"data row1 col11\" >-0.4459%</td>\n",
       "                        <td id=\"T_73060_row1_col12\" class=\"data row1 col12\" >-0.4651%</td>\n",
       "                        <td id=\"T_73060_row1_col13\" class=\"data row1 col13\" >-0.2146%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_73060_level0_row2\" class=\"row_heading level0 row2\" >2017-11-14 00:00:00</th>\n",
       "                        <td id=\"T_73060_row2_col0\" class=\"data row2 col0\" >-3.7020%</td>\n",
       "                        <td id=\"T_73060_row2_col1\" class=\"data row2 col1\" >-1.0470%</td>\n",
       "                        <td id=\"T_73060_row2_col2\" class=\"data row2 col2\" >1.0800%</td>\n",
       "                        <td id=\"T_73060_row2_col3\" class=\"data row2 col3\" >0.8975%</td>\n",
       "                        <td id=\"T_73060_row2_col4\" class=\"data row2 col4\" >-0.1548%</td>\n",
       "                        <td id=\"T_73060_row2_col5\" class=\"data row2 col5\" >0.2668%</td>\n",
       "                        <td id=\"T_73060_row2_col6\" class=\"data row2 col6\" >2.6950%</td>\n",
       "                        <td id=\"T_73060_row2_col7\" class=\"data row2 col7\" >0.2320%</td>\n",
       "                        <td id=\"T_73060_row2_col8\" class=\"data row2 col8\" >0.0236%</td>\n",
       "                        <td id=\"T_73060_row2_col9\" class=\"data row2 col9\" >0.1040%</td>\n",
       "                        <td id=\"T_73060_row2_col10\" class=\"data row2 col10\" >0.2373%</td>\n",
       "                        <td id=\"T_73060_row2_col11\" class=\"data row2 col11\" >-0.2741%</td>\n",
       "                        <td id=\"T_73060_row2_col12\" class=\"data row2 col12\" >-0.3932%</td>\n",
       "                        <td id=\"T_73060_row2_col13\" class=\"data row2 col13\" >0.2151%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_73060_level0_row3\" class=\"row_heading level0 row3\" >2017-11-13 00:00:00</th>\n",
       "                        <td id=\"T_73060_row3_col0\" class=\"data row3 col0\" >-1.4503%</td>\n",
       "                        <td id=\"T_73060_row3_col1\" class=\"data row3 col1\" >1.0855%</td>\n",
       "                        <td id=\"T_73060_row3_col2\" class=\"data row3 col2\" >0.7480%</td>\n",
       "                        <td id=\"T_73060_row3_col3\" class=\"data row3 col3\" >-0.2826%</td>\n",
       "                        <td id=\"T_73060_row3_col4\" class=\"data row3 col4\" >0.8179%</td>\n",
       "                        <td id=\"T_73060_row3_col5\" class=\"data row3 col5\" >1.4205%</td>\n",
       "                        <td id=\"T_73060_row3_col6\" class=\"data row3 col6\" >3.4145%</td>\n",
       "                        <td id=\"T_73060_row3_col7\" class=\"data row3 col7\" >0.0906%</td>\n",
       "                        <td id=\"T_73060_row3_col8\" class=\"data row3 col8\" >0.0064%</td>\n",
       "                        <td id=\"T_73060_row3_col9\" class=\"data row3 col9\" >-0.0767%</td>\n",
       "                        <td id=\"T_73060_row3_col10\" class=\"data row3 col10\" >0.0354%</td>\n",
       "                        <td id=\"T_73060_row3_col11\" class=\"data row3 col11\" >-0.0835%</td>\n",
       "                        <td id=\"T_73060_row3_col12\" class=\"data row3 col12\" >-0.1114%</td>\n",
       "                        <td id=\"T_73060_row3_col13\" class=\"data row3 col13\" >-0.0081%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_73060_level0_row4\" class=\"row_heading level0 row4\" >2017-11-10 00:00:00</th>\n",
       "                        <td id=\"T_73060_row4_col0\" class=\"data row4 col0\" >-2.4536%</td>\n",
       "                        <td id=\"T_73060_row4_col1\" class=\"data row4 col1\" >0.7932%</td>\n",
       "                        <td id=\"T_73060_row4_col2\" class=\"data row4 col2\" >-1.3418%</td>\n",
       "                        <td id=\"T_73060_row4_col3\" class=\"data row4 col3\" >-0.5155%</td>\n",
       "                        <td id=\"T_73060_row4_col4\" class=\"data row4 col4\" >0.0710%</td>\n",
       "                        <td id=\"T_73060_row4_col5\" class=\"data row4 col5\" >-0.9381%</td>\n",
       "                        <td id=\"T_73060_row4_col6\" class=\"data row4 col6\" >-0.0910%</td>\n",
       "                        <td id=\"T_73060_row4_col7\" class=\"data row4 col7\" >0.1194%</td>\n",
       "                        <td id=\"T_73060_row4_col8\" class=\"data row4 col8\" >0.3792%</td>\n",
       "                        <td id=\"T_73060_row4_col9\" class=\"data row4 col9\" >0.3047%</td>\n",
       "                        <td id=\"T_73060_row4_col10\" class=\"data row4 col10\" >0.1355%</td>\n",
       "                        <td id=\"T_73060_row4_col11\" class=\"data row4 col11\" >0.7118%</td>\n",
       "                        <td id=\"T_73060_row4_col12\" class=\"data row4 col12\" >0.8207%</td>\n",
       "                        <td id=\"T_73060_row4_col13\" class=\"data row4 col13\" >-0.0090%</td>\n",
       "            </tr>\n",
       "    </tbody></table>"
      ],
      "text/plain": [
       "<pandas.io.formats.style.Styler at 0x7ff27f52a130>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "########################################################################\n",
    "# Uploading Data\n",
    "########################################################################\n",
    "\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "# Interest Rates Data\n",
    "kr = pd.read_excel('KeyRates.xlsx', engine='openpyxl', index_col=0, header=0)/100\n",
    "\n",
    "# Prices  Data\n",
    "assets = pd.read_excel('Assets.xlsx', engine='openpyxl', index_col=0, header=0)\n",
    "\n",
    "# Find common dates\n",
    "a = pd.merge(left=assets, right=kr, how='inner', on='Date')\n",
    "dates = a.index\n",
    "\n",
    "# Calculate interest rates returns\n",
    "kr_returns = kr.loc[dates,:].sort_index().diff().dropna()\n",
    "kr_returns.sort_index(ascending=False, inplace=True)\n",
    "\n",
    "# List of instruments\n",
    "equity = ['APA','CMCSA','CNP','HPQ','PSA','SEE','ZION']\n",
    "bonds = ['PEP11900D031', 'PEP13000D012', 'PEP13000M088',\n",
    "         'PEP23900M103','PEP70101M530','PEP70101M571',\n",
    "         'PEP70310M156']\n",
    "\n",
    "# Calculate assets returns\n",
    "assets_returns = assets.loc[dates, equity + bonds]\n",
    "assets_returns = assets_returns.sort_index().pct_change().dropna()\n",
    "assets_returns.sort_index(ascending=False, inplace=True)\n",
    "\n",
    "# Show tables\n",
    "display(kr_returns.head().style.format(\"{:.4%}\"))\n",
    "display(assets_returns.head().style.format(\"{:.4%}\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Durations Matrix\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<style  type=\"text/css\" >\n",
       "#T_8ff86_row0_col0{\n",
       "            background-color:  #006034;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_8ff86_row0_col1{\n",
       "            background-color:  #2c8f4b;\n",
       "            color:  #000000;\n",
       "        }#T_8ff86_row0_col2,#T_8ff86_row0_col3,#T_8ff86_row0_col4,#T_8ff86_row1_col1,#T_8ff86_row2_col0,#T_8ff86_row3_col5,#T_8ff86_row4_col6,#T_8ff86_row4_col7{\n",
       "            background-color:  #004529;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_8ff86_row0_col5{\n",
       "            background-color:  #2f944e;\n",
       "            color:  #000000;\n",
       "        }#T_8ff86_row0_col6,#T_8ff86_row0_col7,#T_8ff86_row0_col8,#T_8ff86_row1_col0,#T_8ff86_row1_col8,#T_8ff86_row2_col1,#T_8ff86_row2_col7,#T_8ff86_row2_col8,#T_8ff86_row3_col0,#T_8ff86_row3_col3,#T_8ff86_row3_col4,#T_8ff86_row3_col7,#T_8ff86_row3_col8,#T_8ff86_row4_col0,#T_8ff86_row4_col2,#T_8ff86_row4_col5,#T_8ff86_row4_col8{\n",
       "            background-color:  #ffffe5;\n",
       "            color:  #000000;\n",
       "        }#T_8ff86_row1_col2{\n",
       "            background-color:  #8ed082;\n",
       "            color:  #000000;\n",
       "        }#T_8ff86_row1_col3{\n",
       "            background-color:  #3ba358;\n",
       "            color:  #000000;\n",
       "        }#T_8ff86_row1_col4{\n",
       "            background-color:  #238443;\n",
       "            color:  #000000;\n",
       "        }#T_8ff86_row1_col5{\n",
       "            background-color:  #fbfdce;\n",
       "            color:  #000000;\n",
       "        }#T_8ff86_row1_col6{\n",
       "            background-color:  #004a2b;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_8ff86_row1_col7{\n",
       "            background-color:  #fbfed2;\n",
       "            color:  #000000;\n",
       "        }#T_8ff86_row2_col2{\n",
       "            background-color:  #77c679;\n",
       "            color:  #000000;\n",
       "        }#T_8ff86_row2_col3{\n",
       "            background-color:  #cbea9c;\n",
       "            color:  #000000;\n",
       "        }#T_8ff86_row2_col4{\n",
       "            background-color:  #b6e192;\n",
       "            color:  #000000;\n",
       "        }#T_8ff86_row2_col5{\n",
       "            background-color:  #74c477;\n",
       "            color:  #000000;\n",
       "        }#T_8ff86_row2_col6{\n",
       "            background-color:  #40aa5c;\n",
       "            color:  #000000;\n",
       "        }#T_8ff86_row3_col1{\n",
       "            background-color:  #feffde;\n",
       "            color:  #000000;\n",
       "        }#T_8ff86_row3_col2{\n",
       "            background-color:  #eaf7af;\n",
       "            color:  #000000;\n",
       "        }#T_8ff86_row3_col6{\n",
       "            background-color:  #ffffe4;\n",
       "            color:  #000000;\n",
       "        }#T_8ff86_row4_col1{\n",
       "            background-color:  #39a056;\n",
       "            color:  #000000;\n",
       "        }#T_8ff86_row4_col3{\n",
       "            background-color:  #f8fcbe;\n",
       "            color:  #000000;\n",
       "        }#T_8ff86_row4_col4{\n",
       "            background-color:  #f1fab5;\n",
       "            color:  #000000;\n",
       "        }</style><table id=\"T_8ff86_\" ><thead>    <tr>        <th class=\"blank level0\" ></th>        <th class=\"col_heading level0 col0\" >R 0</th>        <th class=\"col_heading level0 col1\" >R 90</th>        <th class=\"col_heading level0 col2\" >R 180</th>        <th class=\"col_heading level0 col3\" >R 360</th>        <th class=\"col_heading level0 col4\" >R 720</th>        <th class=\"col_heading level0 col5\" >R 1800</th>        <th class=\"col_heading level0 col6\" >R 3600</th>        <th class=\"col_heading level0 col7\" >R 7200</th>        <th class=\"col_heading level0 col8\" >R 10800</th>    </tr></thead><tbody>\n",
       "                <tr>\n",
       "                        <th id=\"T_8ff86_level0_row0\" class=\"row_heading level0 row0\" >PEP11900D031</th>\n",
       "                        <td id=\"T_8ff86_row0_col0\" class=\"data row0 col0\" >0.0012</td>\n",
       "                        <td id=\"T_8ff86_row0_col1\" class=\"data row0 col1\" >0.0057</td>\n",
       "                        <td id=\"T_8ff86_row0_col2\" class=\"data row0 col2\" >0.0192</td>\n",
       "                        <td id=\"T_8ff86_row0_col3\" class=\"data row0 col3\" >0.0730</td>\n",
       "                        <td id=\"T_8ff86_row0_col4\" class=\"data row0 col4\" >0.3685</td>\n",
       "                        <td id=\"T_8ff86_row0_col5\" class=\"data row0 col5\" >3.0416</td>\n",
       "                        <td id=\"T_8ff86_row0_col6\" class=\"data row0 col6\" >0.0030</td>\n",
       "                        <td id=\"T_8ff86_row0_col7\" class=\"data row0 col7\" >0.0000</td>\n",
       "                        <td id=\"T_8ff86_row0_col8\" class=\"data row0 col8\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_8ff86_level0_row1\" class=\"row_heading level0 row1\" >PEP13000D012</th>\n",
       "                        <td id=\"T_8ff86_row1_col0\" class=\"data row1 col0\" >0.0000</td>\n",
       "                        <td id=\"T_8ff86_row1_col1\" class=\"data row1 col1\" >0.0078</td>\n",
       "                        <td id=\"T_8ff86_row1_col2\" class=\"data row1 col2\" >0.0142</td>\n",
       "                        <td id=\"T_8ff86_row1_col3\" class=\"data row1 col3\" >0.0617</td>\n",
       "                        <td id=\"T_8ff86_row1_col4\" class=\"data row1 col4\" >0.3327</td>\n",
       "                        <td id=\"T_8ff86_row1_col5\" class=\"data row1 col5\" >1.0902</td>\n",
       "                        <td id=\"T_8ff86_row1_col6\" class=\"data row1 col6\" >4.8055</td>\n",
       "                        <td id=\"T_8ff86_row1_col7\" class=\"data row1 col7\" >0.2074</td>\n",
       "                        <td id=\"T_8ff86_row1_col8\" class=\"data row1 col8\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_8ff86_level0_row2\" class=\"row_heading level0 row2\" >PEP13000M088</th>\n",
       "                        <td id=\"T_8ff86_row2_col0\" class=\"data row2 col0\" >0.0013</td>\n",
       "                        <td id=\"T_8ff86_row2_col1\" class=\"data row2 col1\" >0.0004</td>\n",
       "                        <td id=\"T_8ff86_row2_col2\" class=\"data row2 col2\" >0.0147</td>\n",
       "                        <td id=\"T_8ff86_row2_col3\" class=\"data row2 col3\" >0.0501</td>\n",
       "                        <td id=\"T_8ff86_row2_col4\" class=\"data row2 col4\" >0.2770</td>\n",
       "                        <td id=\"T_8ff86_row2_col5\" class=\"data row2 col5\" >2.4626</td>\n",
       "                        <td id=\"T_8ff86_row2_col6\" class=\"data row2 col6\" >3.0764</td>\n",
       "                        <td id=\"T_8ff86_row2_col7\" class=\"data row2 col7\" >0.0000</td>\n",
       "                        <td id=\"T_8ff86_row2_col8\" class=\"data row2 col8\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_8ff86_level0_row3\" class=\"row_heading level0 row3\" >PEP23900M103</th>\n",
       "                        <td id=\"T_8ff86_row3_col0\" class=\"data row3 col0\" >0.0000</td>\n",
       "                        <td id=\"T_8ff86_row3_col1\" class=\"data row3 col1\" >0.0005</td>\n",
       "                        <td id=\"T_8ff86_row3_col2\" class=\"data row3 col2\" >0.0117</td>\n",
       "                        <td id=\"T_8ff86_row3_col3\" class=\"data row3 col3\" >0.0405</td>\n",
       "                        <td id=\"T_8ff86_row3_col4\" class=\"data row3 col4\" >0.2274</td>\n",
       "                        <td id=\"T_8ff86_row3_col5\" class=\"data row3 col5\" >3.9726</td>\n",
       "                        <td id=\"T_8ff86_row3_col6\" class=\"data row3 col6\" >0.0381</td>\n",
       "                        <td id=\"T_8ff86_row3_col7\" class=\"data row3 col7\" >0.0000</td>\n",
       "                        <td id=\"T_8ff86_row3_col8\" class=\"data row3 col8\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_8ff86_level0_row4\" class=\"row_heading level0 row4\" >PEP70101M530</th>\n",
       "                        <td id=\"T_8ff86_row4_col0\" class=\"data row4 col0\" >0.0000</td>\n",
       "                        <td id=\"T_8ff86_row4_col1\" class=\"data row4 col1\" >0.0052</td>\n",
       "                        <td id=\"T_8ff86_row4_col2\" class=\"data row4 col2\" >0.0101</td>\n",
       "                        <td id=\"T_8ff86_row4_col3\" class=\"data row4 col3\" >0.0442</td>\n",
       "                        <td id=\"T_8ff86_row4_col4\" class=\"data row4 col4\" >0.2488</td>\n",
       "                        <td id=\"T_8ff86_row4_col5\" class=\"data row4 col5\" >0.8826</td>\n",
       "                        <td id=\"T_8ff86_row4_col6\" class=\"data row4 col6\" >4.9147</td>\n",
       "                        <td id=\"T_8ff86_row4_col7\" class=\"data row4 col7\" >3.5537</td>\n",
       "                        <td id=\"T_8ff86_row4_col8\" class=\"data row4 col8\" >0.0000</td>\n",
       "            </tr>\n",
       "    </tbody></table>"
      ],
      "text/plain": [
       "<pandas.io.formats.style.Styler at 0x7ff27dade8b0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Convexity Matrix\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<style  type=\"text/css\" >\n",
       "#T_820fb_row0_col0{\n",
       "            background-color:  #b9e294;\n",
       "            color:  #000000;\n",
       "        }#T_820fb_row0_col1{\n",
       "            background-color:  #5db96b;\n",
       "            color:  #000000;\n",
       "        }#T_820fb_row0_col2{\n",
       "            background-color:  #10743c;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_820fb_row0_col3,#T_820fb_row0_col4,#T_820fb_row1_col1,#T_820fb_row1_col6,#T_820fb_row2_col0,#T_820fb_row2_col2,#T_820fb_row3_col5,#T_820fb_row4_col7{\n",
       "            background-color:  #004529;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_820fb_row0_col5{\n",
       "            background-color:  #3da559;\n",
       "            color:  #000000;\n",
       "        }#T_820fb_row0_col6,#T_820fb_row0_col7,#T_820fb_row0_col8,#T_820fb_row1_col0,#T_820fb_row1_col8,#T_820fb_row2_col7,#T_820fb_row2_col8,#T_820fb_row3_col0,#T_820fb_row3_col1,#T_820fb_row3_col4,#T_820fb_row3_col6,#T_820fb_row3_col7,#T_820fb_row3_col8,#T_820fb_row4_col0,#T_820fb_row4_col2,#T_820fb_row4_col3,#T_820fb_row4_col5,#T_820fb_row4_col8{\n",
       "            background-color:  #ffffe5;\n",
       "            color:  #000000;\n",
       "        }#T_820fb_row1_col2{\n",
       "            background-color:  #f6fcb8;\n",
       "            color:  #000000;\n",
       "        }#T_820fb_row1_col3{\n",
       "            background-color:  #69bf72;\n",
       "            color:  #000000;\n",
       "        }#T_820fb_row1_col4{\n",
       "            background-color:  #167a3f;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_820fb_row1_col5{\n",
       "            background-color:  #fcfed7;\n",
       "            color:  #000000;\n",
       "        }#T_820fb_row1_col7{\n",
       "            background-color:  #ffffe4;\n",
       "            color:  #000000;\n",
       "        }#T_820fb_row2_col1{\n",
       "            background-color:  #feffe2;\n",
       "            color:  #000000;\n",
       "        }#T_820fb_row2_col3{\n",
       "            background-color:  #81ca7d;\n",
       "            color:  #000000;\n",
       "        }#T_820fb_row2_col4{\n",
       "            background-color:  #a7db8c;\n",
       "            color:  #000000;\n",
       "        }#T_820fb_row2_col5{\n",
       "            background-color:  #cfec9e;\n",
       "            color:  #000000;\n",
       "        }#T_820fb_row2_col6{\n",
       "            background-color:  #b2df90;\n",
       "            color:  #000000;\n",
       "        }#T_820fb_row3_col2{\n",
       "            background-color:  #258745;\n",
       "            color:  #000000;\n",
       "        }#T_820fb_row3_col3{\n",
       "            background-color:  #def2a7;\n",
       "            color:  #000000;\n",
       "        }#T_820fb_row4_col1{\n",
       "            background-color:  #3aa257;\n",
       "            color:  #000000;\n",
       "        }#T_820fb_row4_col4{\n",
       "            background-color:  #f3fab6;\n",
       "            color:  #000000;\n",
       "        }#T_820fb_row4_col6{\n",
       "            background-color:  #086e3a;\n",
       "            color:  #f1f1f1;\n",
       "        }</style><table id=\"T_820fb_\" ><thead>    <tr>        <th class=\"blank level0\" ></th>        <th class=\"col_heading level0 col0\" >R^2 0</th>        <th class=\"col_heading level0 col1\" >R^2 90</th>        <th class=\"col_heading level0 col2\" >R^2 180</th>        <th class=\"col_heading level0 col3\" >R^2 360</th>        <th class=\"col_heading level0 col4\" >R^2 720</th>        <th class=\"col_heading level0 col5\" >R^2 1800</th>        <th class=\"col_heading level0 col6\" >R^2 3600</th>        <th class=\"col_heading level0 col7\" >R^2 7200</th>        <th class=\"col_heading level0 col8\" >R^2 10800</th>    </tr></thead><tbody>\n",
       "                <tr>\n",
       "                        <th id=\"T_820fb_level0_row0\" class=\"row_heading level0 row0\" >PEP11900D031</th>\n",
       "                        <td id=\"T_820fb_row0_col0\" class=\"data row0 col0\" >0.0004</td>\n",
       "                        <td id=\"T_820fb_row0_col1\" class=\"data row0 col1\" >0.0032</td>\n",
       "                        <td id=\"T_820fb_row0_col2\" class=\"data row0 col2\" >0.0167</td>\n",
       "                        <td id=\"T_820fb_row0_col3\" class=\"data row0 col3\" >0.0928</td>\n",
       "                        <td id=\"T_820fb_row0_col4\" class=\"data row0 col4\" >0.7741</td>\n",
       "                        <td id=\"T_820fb_row0_col5\" class=\"data row0 col5\" >15.5617</td>\n",
       "                        <td id=\"T_820fb_row0_col6\" class=\"data row0 col6\" >0.0000</td>\n",
       "                        <td id=\"T_820fb_row0_col7\" class=\"data row0 col7\" >0.0000</td>\n",
       "                        <td id=\"T_820fb_row0_col8\" class=\"data row0 col8\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_820fb_level0_row1\" class=\"row_heading level0 row1\" >PEP13000D012</th>\n",
       "                        <td id=\"T_820fb_row1_col0\" class=\"data row1 col0\" >0.0000</td>\n",
       "                        <td id=\"T_820fb_row1_col1\" class=\"data row1 col1\" >0.0057</td>\n",
       "                        <td id=\"T_820fb_row1_col2\" class=\"data row1 col2\" >0.0070</td>\n",
       "                        <td id=\"T_820fb_row1_col3\" class=\"data row1 col3\" >0.0756</td>\n",
       "                        <td id=\"T_820fb_row1_col4\" class=\"data row1 col4\" >0.7210</td>\n",
       "                        <td id=\"T_820fb_row1_col5\" class=\"data row1 col5\" >4.4984</td>\n",
       "                        <td id=\"T_820fb_row1_col6\" class=\"data row1 col6\" >45.2159</td>\n",
       "                        <td id=\"T_820fb_row1_col7\" class=\"data row1 col7\" >0.1105</td>\n",
       "                        <td id=\"T_820fb_row1_col8\" class=\"data row1 col8\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_820fb_level0_row2\" class=\"row_heading level0 row2\" >PEP13000M088</th>\n",
       "                        <td id=\"T_820fb_row2_col0\" class=\"data row2 col0\" >0.0010</td>\n",
       "                        <td id=\"T_820fb_row2_col1\" class=\"data row2 col1\" >0.0001</td>\n",
       "                        <td id=\"T_820fb_row2_col2\" class=\"data row2 col2\" >0.0192</td>\n",
       "                        <td id=\"T_820fb_row2_col3\" class=\"data row2 col3\" >0.0736</td>\n",
       "                        <td id=\"T_820fb_row2_col4\" class=\"data row2 col4\" >0.6161</td>\n",
       "                        <td id=\"T_820fb_row2_col5\" class=\"data row2 col5\" >8.8479</td>\n",
       "                        <td id=\"T_820fb_row2_col6\" class=\"data row2 col6\" >16.2880</td>\n",
       "                        <td id=\"T_820fb_row2_col7\" class=\"data row2 col7\" >0.0000</td>\n",
       "                        <td id=\"T_820fb_row2_col8\" class=\"data row2 col8\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_820fb_level0_row3\" class=\"row_heading level0 row3\" >PEP23900M103</th>\n",
       "                        <td id=\"T_820fb_row3_col0\" class=\"data row3 col0\" >0.0000</td>\n",
       "                        <td id=\"T_820fb_row3_col1\" class=\"data row3 col1\" >0.0000</td>\n",
       "                        <td id=\"T_820fb_row3_col2\" class=\"data row3 col2\" >0.0156</td>\n",
       "                        <td id=\"T_820fb_row3_col3\" class=\"data row3 col3\" >0.0644</td>\n",
       "                        <td id=\"T_820fb_row3_col4\" class=\"data row3 col4\" >0.5161</td>\n",
       "                        <td id=\"T_820fb_row3_col5\" class=\"data row3 col5\" >22.1272</td>\n",
       "                        <td id=\"T_820fb_row3_col6\" class=\"data row3 col6\" >0.0022</td>\n",
       "                        <td id=\"T_820fb_row3_col7\" class=\"data row3 col7\" >0.0000</td>\n",
       "                        <td id=\"T_820fb_row3_col8\" class=\"data row3 col8\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_820fb_level0_row4\" class=\"row_heading level0 row4\" >PEP70101M530</th>\n",
       "                        <td id=\"T_820fb_row4_col0\" class=\"data row4 col0\" >0.0000</td>\n",
       "                        <td id=\"T_820fb_row4_col1\" class=\"data row4 col1\" >0.0038</td>\n",
       "                        <td id=\"T_820fb_row4_col2\" class=\"data row4 col2\" >0.0052</td>\n",
       "                        <td id=\"T_820fb_row4_col3\" class=\"data row4 col3\" >0.0561</td>\n",
       "                        <td id=\"T_820fb_row4_col4\" class=\"data row4 col4\" >0.5530</td>\n",
       "                        <td id=\"T_820fb_row4_col5\" class=\"data row4 col5\" >3.7373</td>\n",
       "                        <td id=\"T_820fb_row4_col6\" class=\"data row4 col6\" >38.2315</td>\n",
       "                        <td id=\"T_820fb_row4_col7\" class=\"data row4 col7\" >26.1464</td>\n",
       "                        <td id=\"T_820fb_row4_col8\" class=\"data row4 col8\" >0.0000</td>\n",
       "            </tr>\n",
       "    </tbody></table>"
      ],
      "text/plain": [
       "<pandas.io.formats.style.Styler at 0x7ff2806f4700>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "########################################################################\n",
    "# Uploading Duration and Convexity Matrixes\n",
    "########################################################################\n",
    "\n",
    "durations = pd.read_excel('durations.xlsx', index_col=0, header=0)\n",
    "convexity = pd.read_excel('convexity.xlsx', index_col=0, header=0)\n",
    "\n",
    "print('Durations Matrix')\n",
    "display(durations.head().style.format(\"{:.4f}\").background_gradient(cmap='YlGn'))\n",
    "print('')\n",
    "print('Convexity Matrix')\n",
    "display(convexity.head().style.format(\"{:.4f}\").background_gradient(cmap='YlGn'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Estimating Mean Variance Portfolio\n",
    "\n",
    "### 2.1 Building the loadings matrix and risk factors returns.\n",
    "\n",
    "This part shows how to build a personalized loadings matrix that will be used by __Riskfolio-Lib__ to calculate the expected returns and covariance matrix."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style  type=\"text/css\" >\n",
       "#T_a749c_row0_col0{\n",
       "            background-color:  #e2f4aa;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row0_col1{\n",
       "            background-color:  #ccea9d;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row0_col2,#T_a749c_row0_col3,#T_a749c_row0_col4,#T_a749c_row0_col15,#T_a749c_row0_col16,#T_a749c_row0_col17,#T_a749c_row1_col1,#T_a749c_row1_col9,#T_a749c_row1_col16,#T_a749c_row1_col17,#T_a749c_row2_col16,#T_a749c_row2_col17,#T_a749c_row3_col5,#T_a749c_row3_col9,#T_a749c_row3_col10,#T_a749c_row3_col15,#T_a749c_row3_col16,#T_a749c_row3_col17,#T_a749c_row4_col6,#T_a749c_row4_col9,#T_a749c_row4_col11,#T_a749c_row4_col14,#T_a749c_row4_col17,#T_a749c_row5_col0,#T_a749c_row5_col7,#T_a749c_row5_col8,#T_a749c_row6_col9,#T_a749c_row6_col12,#T_a749c_row6_col13,#T_a749c_row6_col15,#T_a749c_row6_col16,#T_a749c_row6_col17{\n",
       "            background-color:  #ffffe5;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row0_col5{\n",
       "            background-color:  #c7e89a;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row0_col6,#T_a749c_row0_col7,#T_a749c_row0_col8,#T_a749c_row0_col12,#T_a749c_row0_col13,#T_a749c_row1_col0,#T_a749c_row1_col8,#T_a749c_row1_col10,#T_a749c_row1_col15,#T_a749c_row2_col1,#T_a749c_row2_col7,#T_a749c_row2_col8,#T_a749c_row2_col9,#T_a749c_row2_col11,#T_a749c_row3_col0,#T_a749c_row3_col4,#T_a749c_row3_col7,#T_a749c_row3_col8,#T_a749c_row3_col14,#T_a749c_row4_col0,#T_a749c_row4_col5,#T_a749c_row4_col8,#T_a749c_row5_col16,#T_a749c_row5_col17,#T_a749c_row6_col0,#T_a749c_row6_col2,#T_a749c_row6_col3,#T_a749c_row6_col6,#T_a749c_row6_col7,#T_a749c_row6_col8{\n",
       "            background-color:  #004529;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_a749c_row0_col9,#T_a749c_row4_col1{\n",
       "            background-color:  #b9e294;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row0_col10,#T_a749c_row1_col12,#T_a749c_row5_col10{\n",
       "            background-color:  #5db96b;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row0_col11{\n",
       "            background-color:  #10743c;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_a749c_row0_col14{\n",
       "            background-color:  #3da559;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row1_col2{\n",
       "            background-color:  #6bc072;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row1_col3{\n",
       "            background-color:  #b8e293;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row1_col4{\n",
       "            background-color:  #d9f0a3;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row1_col5{\n",
       "            background-color:  #005830;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_a749c_row1_col6{\n",
       "            background-color:  #feffde;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row1_col7{\n",
       "            background-color:  #004e2d;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_a749c_row1_col11{\n",
       "            background-color:  #f6fcb8;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row1_col13{\n",
       "            background-color:  #13773d;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_a749c_row1_col14{\n",
       "            background-color:  #fcfed7;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row2_col0{\n",
       "            background-color:  #f7fcb9;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row2_col2{\n",
       "            background-color:  #81ca7d;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row2_col3,#T_a749c_row5_col3{\n",
       "            background-color:  #2f934d;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row2_col4{\n",
       "            background-color:  #3ba358;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row2_col5{\n",
       "            background-color:  #7cc87b;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row2_col6{\n",
       "            background-color:  #aedd8e;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row2_col10{\n",
       "            background-color:  #feffe2;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row2_col12{\n",
       "            background-color:  #74c477;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row2_col13{\n",
       "            background-color:  #9ad587;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row2_col14{\n",
       "            background-color:  #cfec9e;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row2_col15,#T_a749c_row6_col10{\n",
       "            background-color:  #b2df90;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row3_col1{\n",
       "            background-color:  #004a2b;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_a749c_row3_col2{\n",
       "            background-color:  #197c40;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_a749c_row3_col3{\n",
       "            background-color:  #00472a;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_a749c_row3_col6{\n",
       "            background-color:  #004629;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_a749c_row3_col11{\n",
       "            background-color:  #258745;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row3_col12{\n",
       "            background-color:  #d0ec9f;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row3_col13,#T_a749c_row6_col11{\n",
       "            background-color:  #fcfed3;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row4_col2{\n",
       "            background-color:  #00512e;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_a749c_row4_col3{\n",
       "            background-color:  #006636;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_a749c_row4_col4{\n",
       "            background-color:  #076d39;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_a749c_row4_col7{\n",
       "            background-color:  #9dd688;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row4_col10{\n",
       "            background-color:  #3aa257;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row4_col12{\n",
       "            background-color:  #fbfdcf;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row4_col13{\n",
       "            background-color:  #e8f6ae;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row4_col15{\n",
       "            background-color:  #086e3a;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_a749c_row4_col16{\n",
       "            background-color:  #d5eea1;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row5_col1{\n",
       "            background-color:  #6fc174;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row5_col2{\n",
       "            background-color:  #30954f;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row5_col4{\n",
       "            background-color:  #49af61;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row5_col5,#T_a749c_row6_col14{\n",
       "            background-color:  #00502d;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_a749c_row5_col6{\n",
       "            background-color:  #83cb7d;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row5_col9{\n",
       "            background-color:  #89ce80;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row5_col11{\n",
       "            background-color:  #66bd70;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row5_col12{\n",
       "            background-color:  #d6efa2;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row5_col13,#T_a749c_row5_col15{\n",
       "            background-color:  #8dcf81;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row5_col14{\n",
       "            background-color:  #fdfedb;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row6_col1{\n",
       "            background-color:  #6dc073;\n",
       "            color:  #000000;\n",
       "        }#T_a749c_row6_col4{\n",
       "            background-color:  #208242;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_a749c_row6_col5{\n",
       "            background-color:  #fdfedd;\n",
       "            color:  #000000;\n",
       "        }</style><table id=\"T_a749c_\" ><thead>    <tr>        <th class=\"blank level0\" ></th>        <th class=\"col_heading level0 col0\" >R 0</th>        <th class=\"col_heading level0 col1\" >R 90</th>        <th class=\"col_heading level0 col2\" >R 180</th>        <th class=\"col_heading level0 col3\" >R 360</th>        <th class=\"col_heading level0 col4\" >R 720</th>        <th class=\"col_heading level0 col5\" >R 1800</th>        <th class=\"col_heading level0 col6\" >R 3600</th>        <th class=\"col_heading level0 col7\" >R 7200</th>        <th class=\"col_heading level0 col8\" >R 10800</th>        <th class=\"col_heading level0 col9\" >R^2 0</th>        <th class=\"col_heading level0 col10\" >R^2 90</th>        <th class=\"col_heading level0 col11\" >R^2 180</th>        <th class=\"col_heading level0 col12\" >R^2 360</th>        <th class=\"col_heading level0 col13\" >R^2 720</th>        <th class=\"col_heading level0 col14\" >R^2 1800</th>        <th class=\"col_heading level0 col15\" >R^2 3600</th>        <th class=\"col_heading level0 col16\" >R^2 7200</th>        <th class=\"col_heading level0 col17\" >R^2 10800</th>    </tr></thead><tbody>\n",
       "                <tr>\n",
       "                        <th id=\"T_a749c_level0_row0\" class=\"row_heading level0 row0\" >PEP11900D031</th>\n",
       "                        <td id=\"T_a749c_row0_col0\" class=\"data row0 col0\" >-0.0012</td>\n",
       "                        <td id=\"T_a749c_row0_col1\" class=\"data row0 col1\" >-0.0057</td>\n",
       "                        <td id=\"T_a749c_row0_col2\" class=\"data row0 col2\" >-0.0192</td>\n",
       "                        <td id=\"T_a749c_row0_col3\" class=\"data row0 col3\" >-0.0730</td>\n",
       "                        <td id=\"T_a749c_row0_col4\" class=\"data row0 col4\" >-0.3685</td>\n",
       "                        <td id=\"T_a749c_row0_col5\" class=\"data row0 col5\" >-3.0416</td>\n",
       "                        <td id=\"T_a749c_row0_col6\" class=\"data row0 col6\" >-0.0030</td>\n",
       "                        <td id=\"T_a749c_row0_col7\" class=\"data row0 col7\" >-0.0000</td>\n",
       "                        <td id=\"T_a749c_row0_col8\" class=\"data row0 col8\" >-0.0000</td>\n",
       "                        <td id=\"T_a749c_row0_col9\" class=\"data row0 col9\" >0.0002</td>\n",
       "                        <td id=\"T_a749c_row0_col10\" class=\"data row0 col10\" >0.0016</td>\n",
       "                        <td id=\"T_a749c_row0_col11\" class=\"data row0 col11\" >0.0083</td>\n",
       "                        <td id=\"T_a749c_row0_col12\" class=\"data row0 col12\" >0.0464</td>\n",
       "                        <td id=\"T_a749c_row0_col13\" class=\"data row0 col13\" >0.3871</td>\n",
       "                        <td id=\"T_a749c_row0_col14\" class=\"data row0 col14\" >7.7809</td>\n",
       "                        <td id=\"T_a749c_row0_col15\" class=\"data row0 col15\" >0.0000</td>\n",
       "                        <td id=\"T_a749c_row0_col16\" class=\"data row0 col16\" >0.0000</td>\n",
       "                        <td id=\"T_a749c_row0_col17\" class=\"data row0 col17\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_a749c_level0_row1\" class=\"row_heading level0 row1\" >PEP13000D012</th>\n",
       "                        <td id=\"T_a749c_row1_col0\" class=\"data row1 col0\" >-0.0000</td>\n",
       "                        <td id=\"T_a749c_row1_col1\" class=\"data row1 col1\" >-0.0078</td>\n",
       "                        <td id=\"T_a749c_row1_col2\" class=\"data row1 col2\" >-0.0142</td>\n",
       "                        <td id=\"T_a749c_row1_col3\" class=\"data row1 col3\" >-0.0617</td>\n",
       "                        <td id=\"T_a749c_row1_col4\" class=\"data row1 col4\" >-0.3327</td>\n",
       "                        <td id=\"T_a749c_row1_col5\" class=\"data row1 col5\" >-1.0902</td>\n",
       "                        <td id=\"T_a749c_row1_col6\" class=\"data row1 col6\" >-4.8055</td>\n",
       "                        <td id=\"T_a749c_row1_col7\" class=\"data row1 col7\" >-0.2074</td>\n",
       "                        <td id=\"T_a749c_row1_col8\" class=\"data row1 col8\" >-0.0000</td>\n",
       "                        <td id=\"T_a749c_row1_col9\" class=\"data row1 col9\" >0.0000</td>\n",
       "                        <td id=\"T_a749c_row1_col10\" class=\"data row1 col10\" >0.0029</td>\n",
       "                        <td id=\"T_a749c_row1_col11\" class=\"data row1 col11\" >0.0035</td>\n",
       "                        <td id=\"T_a749c_row1_col12\" class=\"data row1 col12\" >0.0378</td>\n",
       "                        <td id=\"T_a749c_row1_col13\" class=\"data row1 col13\" >0.3605</td>\n",
       "                        <td id=\"T_a749c_row1_col14\" class=\"data row1 col14\" >2.2492</td>\n",
       "                        <td id=\"T_a749c_row1_col15\" class=\"data row1 col15\" >22.6080</td>\n",
       "                        <td id=\"T_a749c_row1_col16\" class=\"data row1 col16\" >0.0553</td>\n",
       "                        <td id=\"T_a749c_row1_col17\" class=\"data row1 col17\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_a749c_level0_row2\" class=\"row_heading level0 row2\" >PEP13000M088</th>\n",
       "                        <td id=\"T_a749c_row2_col0\" class=\"data row2 col0\" >-0.0013</td>\n",
       "                        <td id=\"T_a749c_row2_col1\" class=\"data row2 col1\" >-0.0004</td>\n",
       "                        <td id=\"T_a749c_row2_col2\" class=\"data row2 col2\" >-0.0147</td>\n",
       "                        <td id=\"T_a749c_row2_col3\" class=\"data row2 col3\" >-0.0501</td>\n",
       "                        <td id=\"T_a749c_row2_col4\" class=\"data row2 col4\" >-0.2770</td>\n",
       "                        <td id=\"T_a749c_row2_col5\" class=\"data row2 col5\" >-2.4626</td>\n",
       "                        <td id=\"T_a749c_row2_col6\" class=\"data row2 col6\" >-3.0764</td>\n",
       "                        <td id=\"T_a749c_row2_col7\" class=\"data row2 col7\" >-0.0000</td>\n",
       "                        <td id=\"T_a749c_row2_col8\" class=\"data row2 col8\" >-0.0000</td>\n",
       "                        <td id=\"T_a749c_row2_col9\" class=\"data row2 col9\" >0.0005</td>\n",
       "                        <td id=\"T_a749c_row2_col10\" class=\"data row2 col10\" >0.0000</td>\n",
       "                        <td id=\"T_a749c_row2_col11\" class=\"data row2 col11\" >0.0096</td>\n",
       "                        <td id=\"T_a749c_row2_col12\" class=\"data row2 col12\" >0.0368</td>\n",
       "                        <td id=\"T_a749c_row2_col13\" class=\"data row2 col13\" >0.3081</td>\n",
       "                        <td id=\"T_a749c_row2_col14\" class=\"data row2 col14\" >4.4240</td>\n",
       "                        <td id=\"T_a749c_row2_col15\" class=\"data row2 col15\" >8.1440</td>\n",
       "                        <td id=\"T_a749c_row2_col16\" class=\"data row2 col16\" >0.0000</td>\n",
       "                        <td id=\"T_a749c_row2_col17\" class=\"data row2 col17\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_a749c_level0_row3\" class=\"row_heading level0 row3\" >PEP23900M103</th>\n",
       "                        <td id=\"T_a749c_row3_col0\" class=\"data row3 col0\" >-0.0000</td>\n",
       "                        <td id=\"T_a749c_row3_col1\" class=\"data row3 col1\" >-0.0005</td>\n",
       "                        <td id=\"T_a749c_row3_col2\" class=\"data row3 col2\" >-0.0117</td>\n",
       "                        <td id=\"T_a749c_row3_col3\" class=\"data row3 col3\" >-0.0405</td>\n",
       "                        <td id=\"T_a749c_row3_col4\" class=\"data row3 col4\" >-0.2274</td>\n",
       "                        <td id=\"T_a749c_row3_col5\" class=\"data row3 col5\" >-3.9726</td>\n",
       "                        <td id=\"T_a749c_row3_col6\" class=\"data row3 col6\" >-0.0381</td>\n",
       "                        <td id=\"T_a749c_row3_col7\" class=\"data row3 col7\" >-0.0000</td>\n",
       "                        <td id=\"T_a749c_row3_col8\" class=\"data row3 col8\" >-0.0000</td>\n",
       "                        <td id=\"T_a749c_row3_col9\" class=\"data row3 col9\" >0.0000</td>\n",
       "                        <td id=\"T_a749c_row3_col10\" class=\"data row3 col10\" >0.0000</td>\n",
       "                        <td id=\"T_a749c_row3_col11\" class=\"data row3 col11\" >0.0078</td>\n",
       "                        <td id=\"T_a749c_row3_col12\" class=\"data row3 col12\" >0.0322</td>\n",
       "                        <td id=\"T_a749c_row3_col13\" class=\"data row3 col13\" >0.2581</td>\n",
       "                        <td id=\"T_a749c_row3_col14\" class=\"data row3 col14\" >11.0636</td>\n",
       "                        <td id=\"T_a749c_row3_col15\" class=\"data row3 col15\" >0.0011</td>\n",
       "                        <td id=\"T_a749c_row3_col16\" class=\"data row3 col16\" >0.0000</td>\n",
       "                        <td id=\"T_a749c_row3_col17\" class=\"data row3 col17\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_a749c_level0_row4\" class=\"row_heading level0 row4\" >PEP70101M530</th>\n",
       "                        <td id=\"T_a749c_row4_col0\" class=\"data row4 col0\" >-0.0000</td>\n",
       "                        <td id=\"T_a749c_row4_col1\" class=\"data row4 col1\" >-0.0052</td>\n",
       "                        <td id=\"T_a749c_row4_col2\" class=\"data row4 col2\" >-0.0101</td>\n",
       "                        <td id=\"T_a749c_row4_col3\" class=\"data row4 col3\" >-0.0442</td>\n",
       "                        <td id=\"T_a749c_row4_col4\" class=\"data row4 col4\" >-0.2488</td>\n",
       "                        <td id=\"T_a749c_row4_col5\" class=\"data row4 col5\" >-0.8826</td>\n",
       "                        <td id=\"T_a749c_row4_col6\" class=\"data row4 col6\" >-4.9147</td>\n",
       "                        <td id=\"T_a749c_row4_col7\" class=\"data row4 col7\" >-3.5537</td>\n",
       "                        <td id=\"T_a749c_row4_col8\" class=\"data row4 col8\" >-0.0000</td>\n",
       "                        <td id=\"T_a749c_row4_col9\" class=\"data row4 col9\" >0.0000</td>\n",
       "                        <td id=\"T_a749c_row4_col10\" class=\"data row4 col10\" >0.0019</td>\n",
       "                        <td id=\"T_a749c_row4_col11\" class=\"data row4 col11\" >0.0026</td>\n",
       "                        <td id=\"T_a749c_row4_col12\" class=\"data row4 col12\" >0.0280</td>\n",
       "                        <td id=\"T_a749c_row4_col13\" class=\"data row4 col13\" >0.2765</td>\n",
       "                        <td id=\"T_a749c_row4_col14\" class=\"data row4 col14\" >1.8686</td>\n",
       "                        <td id=\"T_a749c_row4_col15\" class=\"data row4 col15\" >19.1157</td>\n",
       "                        <td id=\"T_a749c_row4_col16\" class=\"data row4 col16\" >13.0732</td>\n",
       "                        <td id=\"T_a749c_row4_col17\" class=\"data row4 col17\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_a749c_level0_row5\" class=\"row_heading level0 row5\" >PEP70101M571</th>\n",
       "                        <td id=\"T_a749c_row5_col0\" class=\"data row5 col0\" >-0.0015</td>\n",
       "                        <td id=\"T_a749c_row5_col1\" class=\"data row5 col1\" >-0.0039</td>\n",
       "                        <td id=\"T_a749c_row5_col2\" class=\"data row5 col2\" >-0.0126</td>\n",
       "                        <td id=\"T_a749c_row5_col3\" class=\"data row5 col3\" >-0.0501</td>\n",
       "                        <td id=\"T_a749c_row5_col4\" class=\"data row5 col4\" >-0.2829</td>\n",
       "                        <td id=\"T_a749c_row5_col5\" class=\"data row5 col5\" >-1.0108</td>\n",
       "                        <td id=\"T_a749c_row5_col6\" class=\"data row5 col6\" >-2.5878</td>\n",
       "                        <td id=\"T_a749c_row5_col7\" class=\"data row5 col7\" >-6.0312</td>\n",
       "                        <td id=\"T_a749c_row5_col8\" class=\"data row5 col8\" >-0.4501</td>\n",
       "                        <td id=\"T_a749c_row5_col9\" class=\"data row5 col9\" >0.0002</td>\n",
       "                        <td id=\"T_a749c_row5_col10\" class=\"data row5 col10\" >0.0016</td>\n",
       "                        <td id=\"T_a749c_row5_col11\" class=\"data row5 col11\" >0.0064</td>\n",
       "                        <td id=\"T_a749c_row5_col12\" class=\"data row5 col12\" >0.0319</td>\n",
       "                        <td id=\"T_a749c_row5_col13\" class=\"data row5 col13\" >0.3123</td>\n",
       "                        <td id=\"T_a749c_row5_col14\" class=\"data row5 col14\" >2.1336</td>\n",
       "                        <td id=\"T_a749c_row5_col15\" class=\"data row5 col15\" >10.1632</td>\n",
       "                        <td id=\"T_a749c_row5_col16\" class=\"data row5 col16\" >49.9021</td>\n",
       "                        <td id=\"T_a749c_row5_col17\" class=\"data row5 col17\" >0.4523</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_a749c_level0_row6\" class=\"row_heading level0 row6\" >PEP70310M156</th>\n",
       "                        <td id=\"T_a749c_row6_col0\" class=\"data row6 col0\" >-0.0000</td>\n",
       "                        <td id=\"T_a749c_row6_col1\" class=\"data row6 col1\" >-0.0039</td>\n",
       "                        <td id=\"T_a749c_row6_col2\" class=\"data row6 col2\" >-0.0097</td>\n",
       "                        <td id=\"T_a749c_row6_col3\" class=\"data row6 col3\" >-0.0403</td>\n",
       "                        <td id=\"T_a749c_row6_col4\" class=\"data row6 col4\" >-0.2614</td>\n",
       "                        <td id=\"T_a749c_row6_col5\" class=\"data row6 col5\" >-3.8920</td>\n",
       "                        <td id=\"T_a749c_row6_col6\" class=\"data row6 col6\" >-0.0000</td>\n",
       "                        <td id=\"T_a749c_row6_col7\" class=\"data row6 col7\" >-0.0000</td>\n",
       "                        <td id=\"T_a749c_row6_col8\" class=\"data row6 col8\" >-0.0000</td>\n",
       "                        <td id=\"T_a749c_row6_col9\" class=\"data row6 col9\" >0.0000</td>\n",
       "                        <td id=\"T_a749c_row6_col10\" class=\"data row6 col10\" >0.0010</td>\n",
       "                        <td id=\"T_a749c_row6_col11\" class=\"data row6 col11\" >0.0030</td>\n",
       "                        <td id=\"T_a749c_row6_col12\" class=\"data row6 col12\" >0.0268</td>\n",
       "                        <td id=\"T_a749c_row6_col13\" class=\"data row6 col13\" >0.2508</td>\n",
       "                        <td id=\"T_a749c_row6_col14\" class=\"data row6 col14\" >10.6813</td>\n",
       "                        <td id=\"T_a749c_row6_col15\" class=\"data row6 col15\" >0.0000</td>\n",
       "                        <td id=\"T_a749c_row6_col16\" class=\"data row6 col16\" >0.0000</td>\n",
       "                        <td id=\"T_a749c_row6_col17\" class=\"data row6 col17\" >0.0000</td>\n",
       "            </tr>\n",
       "    </tbody></table>"
      ],
      "text/plain": [
       "<pandas.io.formats.style.Styler at 0x7ff27db7edc0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "########################################################################\n",
    "# Building The Loadings Matrix\n",
    "########################################################################\n",
    "\n",
    "loadings = pd.concat([-1.0 * durations, 0.5 * convexity], axis = 1)\n",
    "\n",
    "display(loadings.style.format(\"{:.4f}\").background_gradient(cmap='YlGn'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style  type=\"text/css\" >\n",
       "</style><table id=\"T_223fa_\" ><thead>    <tr>        <th class=\"blank level0\" ></th>        <th class=\"col_heading level0 col0\" >R 0</th>        <th class=\"col_heading level0 col1\" >R 90</th>        <th class=\"col_heading level0 col2\" >R 180</th>        <th class=\"col_heading level0 col3\" >R 360</th>        <th class=\"col_heading level0 col4\" >R 720</th>        <th class=\"col_heading level0 col5\" >R 1800</th>        <th class=\"col_heading level0 col6\" >R 3600</th>        <th class=\"col_heading level0 col7\" >R 7200</th>        <th class=\"col_heading level0 col8\" >R 10800</th>        <th class=\"col_heading level0 col9\" >R^2 0</th>        <th class=\"col_heading level0 col10\" >R^2 90</th>        <th class=\"col_heading level0 col11\" >R^2 180</th>        <th class=\"col_heading level0 col12\" >R^2 360</th>        <th class=\"col_heading level0 col13\" >R^2 720</th>        <th class=\"col_heading level0 col14\" >R^2 1800</th>        <th class=\"col_heading level0 col15\" >R^2 3600</th>        <th class=\"col_heading level0 col16\" >R^2 7200</th>        <th class=\"col_heading level0 col17\" >R^2 10800</th>    </tr>    <tr>        <th class=\"index_name level0\" >Date</th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>    </tr></thead><tbody>\n",
       "                <tr>\n",
       "                        <th id=\"T_223fa_level0_row0\" class=\"row_heading level0 row0\" >2017-11-16 00:00:00</th>\n",
       "                        <td id=\"T_223fa_row0_col0\" class=\"data row0 col0\" >0.0000%</td>\n",
       "                        <td id=\"T_223fa_row0_col1\" class=\"data row0 col1\" >0.0059%</td>\n",
       "                        <td id=\"T_223fa_row0_col2\" class=\"data row0 col2\" >0.0108%</td>\n",
       "                        <td id=\"T_223fa_row0_col3\" class=\"data row0 col3\" >0.0178%</td>\n",
       "                        <td id=\"T_223fa_row0_col4\" class=\"data row0 col4\" >0.0246%</td>\n",
       "                        <td id=\"T_223fa_row0_col5\" class=\"data row0 col5\" >0.0213%</td>\n",
       "                        <td id=\"T_223fa_row0_col6\" class=\"data row0 col6\" >0.0075%</td>\n",
       "                        <td id=\"T_223fa_row0_col7\" class=\"data row0 col7\" >-0.0048%</td>\n",
       "                        <td id=\"T_223fa_row0_col8\" class=\"data row0 col8\" >-0.0093%</td>\n",
       "                        <td id=\"T_223fa_row0_col9\" class=\"data row0 col9\" >0.0000%</td>\n",
       "                        <td id=\"T_223fa_row0_col10\" class=\"data row0 col10\" >0.0000%</td>\n",
       "                        <td id=\"T_223fa_row0_col11\" class=\"data row0 col11\" >0.0000%</td>\n",
       "                        <td id=\"T_223fa_row0_col12\" class=\"data row0 col12\" >0.0000%</td>\n",
       "                        <td id=\"T_223fa_row0_col13\" class=\"data row0 col13\" >0.0000%</td>\n",
       "                        <td id=\"T_223fa_row0_col14\" class=\"data row0 col14\" >0.0000%</td>\n",
       "                        <td id=\"T_223fa_row0_col15\" class=\"data row0 col15\" >0.0000%</td>\n",
       "                        <td id=\"T_223fa_row0_col16\" class=\"data row0 col16\" >0.0000%</td>\n",
       "                        <td id=\"T_223fa_row0_col17\" class=\"data row0 col17\" >0.0000%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_223fa_level0_row1\" class=\"row_heading level0 row1\" >2017-11-15 00:00:00</th>\n",
       "                        <td id=\"T_223fa_row1_col0\" class=\"data row1 col0\" >0.0180%</td>\n",
       "                        <td id=\"T_223fa_row1_col1\" class=\"data row1 col1\" >0.0247%</td>\n",
       "                        <td id=\"T_223fa_row1_col2\" class=\"data row1 col2\" >0.0303%</td>\n",
       "                        <td id=\"T_223fa_row1_col3\" class=\"data row1 col3\" >0.0391%</td>\n",
       "                        <td id=\"T_223fa_row1_col4\" class=\"data row1 col4\" >0.0495%</td>\n",
       "                        <td id=\"T_223fa_row1_col5\" class=\"data row1 col5\" >0.0558%</td>\n",
       "                        <td id=\"T_223fa_row1_col6\" class=\"data row1 col6\" >0.0512%</td>\n",
       "                        <td id=\"T_223fa_row1_col7\" class=\"data row1 col7\" >0.0450%</td>\n",
       "                        <td id=\"T_223fa_row1_col8\" class=\"data row1 col8\" >0.0417%</td>\n",
       "                        <td id=\"T_223fa_row1_col9\" class=\"data row1 col9\" >0.0000%</td>\n",
       "                        <td id=\"T_223fa_row1_col10\" class=\"data row1 col10\" >0.0000%</td>\n",
       "                        <td id=\"T_223fa_row1_col11\" class=\"data row1 col11\" >0.0000%</td>\n",
       "                        <td id=\"T_223fa_row1_col12\" class=\"data row1 col12\" >0.0000%</td>\n",
       "                        <td id=\"T_223fa_row1_col13\" class=\"data row1 col13\" >0.0000%</td>\n",
       "                        <td id=\"T_223fa_row1_col14\" class=\"data row1 col14\" >0.0000%</td>\n",
       "                        <td id=\"T_223fa_row1_col15\" class=\"data row1 col15\" >0.0000%</td>\n",
       "                        <td id=\"T_223fa_row1_col16\" class=\"data row1 col16\" >0.0000%</td>\n",
       "                        <td id=\"T_223fa_row1_col17\" class=\"data row1 col17\" >0.0000%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_223fa_level0_row2\" class=\"row_heading level0 row2\" >2017-11-14 00:00:00</th>\n",
       "                        <td id=\"T_223fa_row2_col0\" class=\"data row2 col0\" >-0.1800%</td>\n",
       "                        <td id=\"T_223fa_row2_col1\" class=\"data row2 col1\" >-0.1710%</td>\n",
       "                        <td id=\"T_223fa_row2_col2\" class=\"data row2 col2\" >-0.1624%</td>\n",
       "                        <td id=\"T_223fa_row2_col3\" class=\"data row2 col3\" >-0.1460%</td>\n",
       "                        <td id=\"T_223fa_row2_col4\" class=\"data row2 col4\" >-0.1167%</td>\n",
       "                        <td id=\"T_223fa_row2_col5\" class=\"data row2 col5\" >-0.0506%</td>\n",
       "                        <td id=\"T_223fa_row2_col6\" class=\"data row2 col6\" >0.0140%</td>\n",
       "                        <td id=\"T_223fa_row2_col7\" class=\"data row2 col7\" >0.0676%</td>\n",
       "                        <td id=\"T_223fa_row2_col8\" class=\"data row2 col8\" >0.0861%</td>\n",
       "                        <td id=\"T_223fa_row2_col9\" class=\"data row2 col9\" >0.0003%</td>\n",
       "                        <td id=\"T_223fa_row2_col10\" class=\"data row2 col10\" >0.0003%</td>\n",
       "                        <td id=\"T_223fa_row2_col11\" class=\"data row2 col11\" >0.0003%</td>\n",
       "                        <td id=\"T_223fa_row2_col12\" class=\"data row2 col12\" >0.0002%</td>\n",
       "                        <td id=\"T_223fa_row2_col13\" class=\"data row2 col13\" >0.0001%</td>\n",
       "                        <td id=\"T_223fa_row2_col14\" class=\"data row2 col14\" >0.0000%</td>\n",
       "                        <td id=\"T_223fa_row2_col15\" class=\"data row2 col15\" >0.0000%</td>\n",
       "                        <td id=\"T_223fa_row2_col16\" class=\"data row2 col16\" >0.0000%</td>\n",
       "                        <td id=\"T_223fa_row2_col17\" class=\"data row2 col17\" >0.0001%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_223fa_level0_row3\" class=\"row_heading level0 row3\" >2017-11-13 00:00:00</th>\n",
       "                        <td id=\"T_223fa_row3_col0\" class=\"data row3 col0\" >0.0000%</td>\n",
       "                        <td id=\"T_223fa_row3_col1\" class=\"data row3 col1\" >0.0013%</td>\n",
       "                        <td id=\"T_223fa_row3_col2\" class=\"data row3 col2\" >0.0025%</td>\n",
       "                        <td id=\"T_223fa_row3_col3\" class=\"data row3 col3\" >0.0048%</td>\n",
       "                        <td id=\"T_223fa_row3_col4\" class=\"data row3 col4\" >0.0088%</td>\n",
       "                        <td id=\"T_223fa_row3_col5\" class=\"data row3 col5\" >0.0174%</td>\n",
       "                        <td id=\"T_223fa_row3_col6\" class=\"data row3 col6\" >0.0258%</td>\n",
       "                        <td id=\"T_223fa_row3_col7\" class=\"data row3 col7\" >0.0334%</td>\n",
       "                        <td id=\"T_223fa_row3_col8\" class=\"data row3 col8\" >0.0364%</td>\n",
       "                        <td id=\"T_223fa_row3_col9\" class=\"data row3 col9\" >0.0000%</td>\n",
       "                        <td id=\"T_223fa_row3_col10\" class=\"data row3 col10\" >0.0000%</td>\n",
       "                        <td id=\"T_223fa_row3_col11\" class=\"data row3 col11\" >0.0000%</td>\n",
       "                        <td id=\"T_223fa_row3_col12\" class=\"data row3 col12\" >0.0000%</td>\n",
       "                        <td id=\"T_223fa_row3_col13\" class=\"data row3 col13\" >0.0000%</td>\n",
       "                        <td id=\"T_223fa_row3_col14\" class=\"data row3 col14\" >0.0000%</td>\n",
       "                        <td id=\"T_223fa_row3_col15\" class=\"data row3 col15\" >0.0000%</td>\n",
       "                        <td id=\"T_223fa_row3_col16\" class=\"data row3 col16\" >0.0000%</td>\n",
       "                        <td id=\"T_223fa_row3_col17\" class=\"data row3 col17\" >0.0000%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_223fa_level0_row4\" class=\"row_heading level0 row4\" >2017-11-10 00:00:00</th>\n",
       "                        <td id=\"T_223fa_row4_col0\" class=\"data row4 col0\" >0.0000%</td>\n",
       "                        <td id=\"T_223fa_row4_col1\" class=\"data row4 col1\" >0.0026%</td>\n",
       "                        <td id=\"T_223fa_row4_col2\" class=\"data row4 col2\" >0.0043%</td>\n",
       "                        <td id=\"T_223fa_row4_col3\" class=\"data row4 col3\" >0.0054%</td>\n",
       "                        <td id=\"T_223fa_row4_col4\" class=\"data row4 col4\" >0.0017%</td>\n",
       "                        <td id=\"T_223fa_row4_col5\" class=\"data row4 col5\" >-0.0248%</td>\n",
       "                        <td id=\"T_223fa_row4_col6\" class=\"data row4 col6\" >-0.0615%</td>\n",
       "                        <td id=\"T_223fa_row4_col7\" class=\"data row4 col7\" >-0.0936%</td>\n",
       "                        <td id=\"T_223fa_row4_col8\" class=\"data row4 col8\" >-0.1054%</td>\n",
       "                        <td id=\"T_223fa_row4_col9\" class=\"data row4 col9\" >0.0000%</td>\n",
       "                        <td id=\"T_223fa_row4_col10\" class=\"data row4 col10\" >0.0000%</td>\n",
       "                        <td id=\"T_223fa_row4_col11\" class=\"data row4 col11\" >0.0000%</td>\n",
       "                        <td id=\"T_223fa_row4_col12\" class=\"data row4 col12\" >0.0000%</td>\n",
       "                        <td id=\"T_223fa_row4_col13\" class=\"data row4 col13\" >0.0000%</td>\n",
       "                        <td id=\"T_223fa_row4_col14\" class=\"data row4 col14\" >0.0000%</td>\n",
       "                        <td id=\"T_223fa_row4_col15\" class=\"data row4 col15\" >0.0000%</td>\n",
       "                        <td id=\"T_223fa_row4_col16\" class=\"data row4 col16\" >0.0001%</td>\n",
       "                        <td id=\"T_223fa_row4_col17\" class=\"data row4 col17\" >0.0001%</td>\n",
       "            </tr>\n",
       "    </tbody></table>"
      ],
      "text/plain": [
       "<pandas.io.formats.style.Styler at 0x7ff280688fa0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "########################################################################\n",
    "# Building the risk factors returns matrix\n",
    "########################################################################\n",
    "\n",
    "kr_returns_2 =  kr_returns ** 2\n",
    "cols = loadings.columns\n",
    "\n",
    "X = pd.concat([kr_returns, kr_returns_2], axis=1)\n",
    "X.columns = cols\n",
    "\n",
    "display(X.head().style.format(\"{:.4%}\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PEP11900D031</th>\n",
       "      <th>PEP13000D012</th>\n",
       "      <th>PEP13000M088</th>\n",
       "      <th>PEP23900M103</th>\n",
       "      <th>PEP70101M530</th>\n",
       "      <th>PEP70101M571</th>\n",
       "      <th>PEP70310M156</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2017-11-16</th>\n",
       "      <td>-0.000411</td>\n",
       "      <td>-0.000380</td>\n",
       "      <td>-0.000597</td>\n",
       "      <td>-0.000737</td>\n",
       "      <td>-0.000116</td>\n",
       "      <td>0.000076</td>\n",
       "      <td>-0.000633</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-11-15</th>\n",
       "      <td>-0.001626</td>\n",
       "      <td>-0.003076</td>\n",
       "      <td>-0.003041</td>\n",
       "      <td>-0.002286</td>\n",
       "      <td>-0.004459</td>\n",
       "      <td>-0.004651</td>\n",
       "      <td>-0.002146</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-11-14</th>\n",
       "      <td>0.002320</td>\n",
       "      <td>0.000236</td>\n",
       "      <td>0.001040</td>\n",
       "      <td>0.002373</td>\n",
       "      <td>-0.002741</td>\n",
       "      <td>-0.003932</td>\n",
       "      <td>0.002151</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-11-13</th>\n",
       "      <td>0.000906</td>\n",
       "      <td>0.000064</td>\n",
       "      <td>-0.000767</td>\n",
       "      <td>0.000354</td>\n",
       "      <td>-0.000835</td>\n",
       "      <td>-0.001114</td>\n",
       "      <td>-0.000081</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-11-10</th>\n",
       "      <td>0.001194</td>\n",
       "      <td>0.003792</td>\n",
       "      <td>0.003047</td>\n",
       "      <td>0.001355</td>\n",
       "      <td>0.007118</td>\n",
       "      <td>0.008207</td>\n",
       "      <td>-0.000090</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            PEP11900D031  PEP13000D012  PEP13000M088  PEP23900M103  \\\n",
       "Date                                                                 \n",
       "2017-11-16     -0.000411     -0.000380     -0.000597     -0.000737   \n",
       "2017-11-15     -0.001626     -0.003076     -0.003041     -0.002286   \n",
       "2017-11-14      0.002320      0.000236      0.001040      0.002373   \n",
       "2017-11-13      0.000906      0.000064     -0.000767      0.000354   \n",
       "2017-11-10      0.001194      0.003792      0.003047      0.001355   \n",
       "\n",
       "            PEP70101M530  PEP70101M571  PEP70310M156  \n",
       "Date                                                  \n",
       "2017-11-16     -0.000116      0.000076     -0.000633  \n",
       "2017-11-15     -0.004459     -0.004651     -0.002146  \n",
       "2017-11-14     -0.002741     -0.003932      0.002151  \n",
       "2017-11-13     -0.000835     -0.001114     -0.000081  \n",
       "2017-11-10      0.007118      0.008207     -0.000090  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "########################################################################\n",
    "# Building the asset returns matrix\n",
    "########################################################################\n",
    "\n",
    "Y = assets_returns[loadings.index]\n",
    "\n",
    "display(Y.head())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.2 Calculating the portfolio that maximizes Sharpe ratio."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style  type=\"text/css\" >\n",
       "#T_6906f_row0_col0,#T_6906f_row2_col0,#T_6906f_row3_col0,#T_6906f_row6_col0{\n",
       "            background-color:  #ffffe5;\n",
       "            color:  #000000;\n",
       "        }#T_6906f_row1_col0{\n",
       "            background-color:  #f8fcbe;\n",
       "            color:  #000000;\n",
       "        }#T_6906f_row4_col0{\n",
       "            background-color:  #69bf72;\n",
       "            color:  #000000;\n",
       "        }#T_6906f_row5_col0{\n",
       "            background-color:  #004529;\n",
       "            color:  #f1f1f1;\n",
       "        }</style><table id=\"T_6906f_\" ><thead>    <tr>        <th class=\"blank level0\" ></th>        <th class=\"col_heading level0 col0\" >weights</th>    </tr></thead><tbody>\n",
       "                <tr>\n",
       "                        <th id=\"T_6906f_level0_row0\" class=\"row_heading level0 row0\" >PEP11900D031</th>\n",
       "                        <td id=\"T_6906f_row0_col0\" class=\"data row0 col0\" >0.0000%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_6906f_level0_row1\" class=\"row_heading level0 row1\" >PEP13000D012</th>\n",
       "                        <td id=\"T_6906f_row1_col0\" class=\"data row1 col0\" >6.6666%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_6906f_level0_row2\" class=\"row_heading level0 row2\" >PEP13000M088</th>\n",
       "                        <td id=\"T_6906f_row2_col0\" class=\"data row2 col0\" >0.0000%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_6906f_level0_row3\" class=\"row_heading level0 row3\" >PEP23900M103</th>\n",
       "                        <td id=\"T_6906f_row3_col0\" class=\"data row3 col0\" >0.0000%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_6906f_level0_row4\" class=\"row_heading level0 row4\" >PEP70101M530</th>\n",
       "                        <td id=\"T_6906f_row4_col0\" class=\"data row4 col0\" >32.3836%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_6906f_level0_row5\" class=\"row_heading level0 row5\" >PEP70101M571</th>\n",
       "                        <td id=\"T_6906f_row5_col0\" class=\"data row5 col0\" >60.9498%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_6906f_level0_row6\" class=\"row_heading level0 row6\" >PEP70310M156</th>\n",
       "                        <td id=\"T_6906f_row6_col0\" class=\"data row6 col0\" >0.0000%</td>\n",
       "            </tr>\n",
       "    </tbody></table>"
      ],
      "text/plain": [
       "<pandas.io.formats.style.Styler at 0x7ff285015d90>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "########################################################################\n",
    "# Calculating optimum portfolio\n",
    "########################################################################\n",
    "\n",
    "import riskfolio.Portfolio as pf\n",
    "\n",
    "# Building the portfolio object\n",
    "port = pf.Portfolio(returns=Y)\n",
    "\n",
    "# Select method and estimate input parameters:\n",
    "\n",
    "method_mu='hist' # Method to estimate expected returns based on historical data.\n",
    "method_cov='hist' # Method to estimate covariance matrix based on historical data.\n",
    "\n",
    "port.assets_stats(method_mu=method_mu, method_cov=method_cov, d=0.94)\n",
    "\n",
    "port.factors = X\n",
    "port.factors_stats(method_mu=method_mu, method_cov=method_cov, d=0.94, B=loadings)\n",
    "\n",
    "# Estimate optimal portfolio:\n",
    "\n",
    "model='FM' # Factor Model\n",
    "rm = 'MV' # Risk measure used, this time will be variance\n",
    "obj = 'Sharpe' # Objective function, could be MinRisk, MaxRet, Utility or Sharpe\n",
    "hist = False # Use historical scenarios for risk measures that depend on scenarios\n",
    "rf = 0 # Risk free rate\n",
    "l = 0 # Risk aversion factor, only useful when obj is 'Utility'\n",
    "\n",
    "w = port.optimization(model=model, rm=rm, obj=obj, rf=rf, l=l, hist=hist)\n",
    "\n",
    "display(w.style.format(\"{:.4%}\").background_gradient(cmap='YlGn'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Optimization with Key Rate Durations Constraints\n",
    "\n",
    "This part shows how __Riskfolio-Lib__ can be used to build immunized portfolios using __duration matching__ and __convexity matching__, however the example only use duration matching. More information about inmunization theory can be found in this __[link](https://www.investopedia.com/terms/i/immunization.asp)__.\n",
    "\n",
    "### 3.1 Statistics of Risk Factors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style  type=\"text/css\" >\n",
       "#T_c519c_row0_col0,#T_c519c_row0_col1,#T_c519c_row1_col0,#T_c519c_row1_col1,#T_c519c_row2_col0,#T_c519c_row6_col1,#T_c519c_row7_col1,#T_c519c_row8_col1{\n",
       "            background-color:  #004529;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_c519c_row2_col1{\n",
       "            background-color:  #00472a;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_c519c_row3_col0{\n",
       "            background-color:  #00482a;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_c519c_row3_col1{\n",
       "            background-color:  #00512e;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_c519c_row4_col0{\n",
       "            background-color:  #005530;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_c519c_row4_col1{\n",
       "            background-color:  #248644;\n",
       "            color:  #000000;\n",
       "        }#T_c519c_row5_col0{\n",
       "            background-color:  #b9e294;\n",
       "            color:  #000000;\n",
       "        }#T_c519c_row5_col1,#T_c519c_row7_col0{\n",
       "            background-color:  #ffffe5;\n",
       "            color:  #000000;\n",
       "        }#T_c519c_row6_col0{\n",
       "            background-color:  #e9f6af;\n",
       "            color:  #000000;\n",
       "        }#T_c519c_row8_col0{\n",
       "            background-color:  #005a31;\n",
       "            color:  #f1f1f1;\n",
       "        }</style><table id=\"T_c519c_\" ><thead>    <tr>        <th class=\"blank level0\" ></th>        <th class=\"col_heading level0 col0\" >min</th>        <th class=\"col_heading level0 col1\" >max</th>    </tr></thead><tbody>\n",
       "                <tr>\n",
       "                        <th id=\"T_c519c_level0_row0\" class=\"row_heading level0 row0\" >R 0</th>\n",
       "                        <td id=\"T_c519c_row0_col0\" class=\"data row0 col0\" >-0.0015</td>\n",
       "                        <td id=\"T_c519c_row0_col1\" class=\"data row0 col1\" >-0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_c519c_level0_row1\" class=\"row_heading level0 row1\" >R 90</th>\n",
       "                        <td id=\"T_c519c_row1_col0\" class=\"data row1 col0\" >-0.0078</td>\n",
       "                        <td id=\"T_c519c_row1_col1\" class=\"data row1 col1\" >-0.0004</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_c519c_level0_row2\" class=\"row_heading level0 row2\" >R 180</th>\n",
       "                        <td id=\"T_c519c_row2_col0\" class=\"data row2 col0\" >-0.0192</td>\n",
       "                        <td id=\"T_c519c_row2_col1\" class=\"data row2 col1\" >-0.0097</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_c519c_level0_row3\" class=\"row_heading level0 row3\" >R 360</th>\n",
       "                        <td id=\"T_c519c_row3_col0\" class=\"data row3 col0\" >-0.0730</td>\n",
       "                        <td id=\"T_c519c_row3_col1\" class=\"data row3 col1\" >-0.0403</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_c519c_level0_row4\" class=\"row_heading level0 row4\" >R 720</th>\n",
       "                        <td id=\"T_c519c_row4_col0\" class=\"data row4 col0\" >-0.3685</td>\n",
       "                        <td id=\"T_c519c_row4_col1\" class=\"data row4 col1\" >-0.2274</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_c519c_level0_row5\" class=\"row_heading level0 row5\" >R 1800</th>\n",
       "                        <td id=\"T_c519c_row5_col0\" class=\"data row5 col0\" >-3.9726</td>\n",
       "                        <td id=\"T_c519c_row5_col1\" class=\"data row5 col1\" >-0.8826</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_c519c_level0_row6\" class=\"row_heading level0 row6\" >R 3600</th>\n",
       "                        <td id=\"T_c519c_row6_col0\" class=\"data row6 col0\" >-4.9147</td>\n",
       "                        <td id=\"T_c519c_row6_col1\" class=\"data row6 col1\" >-0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_c519c_level0_row7\" class=\"row_heading level0 row7\" >R 7200</th>\n",
       "                        <td id=\"T_c519c_row7_col0\" class=\"data row7 col0\" >-6.0312</td>\n",
       "                        <td id=\"T_c519c_row7_col1\" class=\"data row7 col1\" >-0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_c519c_level0_row8\" class=\"row_heading level0 row8\" >R 10800</th>\n",
       "                        <td id=\"T_c519c_row8_col0\" class=\"data row8 col0\" >-0.4501</td>\n",
       "                        <td id=\"T_c519c_row8_col1\" class=\"data row8 col1\" >-0.0000</td>\n",
       "            </tr>\n",
       "    </tbody></table>"
      ],
      "text/plain": [
       "<pandas.io.formats.style.Styler at 0x7ff2807d57c0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<style  type=\"text/css\" >\n",
       "#T_ed444_row0_col0,#T_ed444_row1_col1,#T_ed444_row2_col2,#T_ed444_row3_col3,#T_ed444_row4_col4,#T_ed444_row5_col5,#T_ed444_row6_col6,#T_ed444_row7_col7,#T_ed444_row8_col8{\n",
       "            background-color:  #004529;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_ed444_row0_col1{\n",
       "            background-color:  #005b32;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_ed444_row0_col2{\n",
       "            background-color:  #2f934d;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row0_col3{\n",
       "            background-color:  #ebf7b0;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row0_col4,#T_ed444_row0_col5,#T_ed444_row0_col6,#T_ed444_row0_col7,#T_ed444_row0_col8,#T_ed444_row1_col8,#T_ed444_row4_col0,#T_ed444_row5_col1,#T_ed444_row6_col1,#T_ed444_row6_col2,#T_ed444_row6_col3,#T_ed444_row7_col1{\n",
       "            background-color:  #ffffe5;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row1_col0{\n",
       "            background-color:  #005931;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_ed444_row1_col2,#T_ed444_row2_col1,#T_ed444_row7_col8,#T_ed444_row8_col7{\n",
       "            background-color:  #005c32;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_ed444_row1_col3{\n",
       "            background-color:  #83cb7d;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row1_col4{\n",
       "            background-color:  #f3fab6;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row1_col5,#T_ed444_row7_col2{\n",
       "            background-color:  #feffe1;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row1_col6,#T_ed444_row1_col7,#T_ed444_row8_col2{\n",
       "            background-color:  #feffdf;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row2_col0{\n",
       "            background-color:  #278946;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row2_col3,#T_ed444_row4_col3{\n",
       "            background-color:  #1d7f41;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_ed444_row2_col4{\n",
       "            background-color:  #abdc8d;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row2_col5,#T_ed444_row6_col0{\n",
       "            background-color:  #fafdcc;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row2_col6{\n",
       "            background-color:  #fdfeda;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row2_col7,#T_ed444_row7_col3{\n",
       "            background-color:  #fcfed7;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row2_col8{\n",
       "            background-color:  #fdfedd;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row3_col0{\n",
       "            background-color:  #d0ec9f;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row3_col1{\n",
       "            background-color:  #7cc87b;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row3_col2{\n",
       "            background-color:  #1c7e40;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_ed444_row3_col4{\n",
       "            background-color:  #15793e;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_ed444_row3_col5{\n",
       "            background-color:  #d7efa2;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row3_col6{\n",
       "            background-color:  #fcfed4;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row3_col7{\n",
       "            background-color:  #f9fdc7;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row3_col8{\n",
       "            background-color:  #fafdc9;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row4_col1{\n",
       "            background-color:  #fbfed2;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row4_col2{\n",
       "            background-color:  #c4e799;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row4_col5{\n",
       "            background-color:  #51b365;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row4_col6{\n",
       "            background-color:  #eff9b3;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row4_col7{\n",
       "            background-color:  #f1fab5;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row4_col8{\n",
       "            background-color:  #f0f9b4;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row5_col0,#T_ed444_row7_col0{\n",
       "            background-color:  #fafdcb;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row5_col2{\n",
       "            background-color:  #fcfed6;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row5_col3,#T_ed444_row6_col4{\n",
       "            background-color:  #e0f3a8;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row5_col4{\n",
       "            background-color:  #42ab5d;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row5_col6,#T_ed444_row6_col5{\n",
       "            background-color:  #288a47;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row5_col7,#T_ed444_row7_col5{\n",
       "            background-color:  #90d083;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row5_col8{\n",
       "            background-color:  #bce395;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row6_col7{\n",
       "            background-color:  #14783e;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_ed444_row6_col8{\n",
       "            background-color:  #4fb264;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row7_col4{\n",
       "            background-color:  #e2f4aa;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row7_col6{\n",
       "            background-color:  #13773d;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_ed444_row8_col0{\n",
       "            background-color:  #f9fdc4;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row8_col1{\n",
       "            background-color:  #feffe2;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row8_col3{\n",
       "            background-color:  #fcfed3;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row8_col4{\n",
       "            background-color:  #ddf2a6;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row8_col5{\n",
       "            background-color:  #b8e293;\n",
       "            color:  #000000;\n",
       "        }#T_ed444_row8_col6{\n",
       "            background-color:  #4cb063;\n",
       "            color:  #000000;\n",
       "        }</style><table id=\"T_ed444_\" ><thead>    <tr>        <th class=\"blank level0\" ></th>        <th class=\"col_heading level0 col0\" >R 0</th>        <th class=\"col_heading level0 col1\" >R 90</th>        <th class=\"col_heading level0 col2\" >R 180</th>        <th class=\"col_heading level0 col3\" >R 360</th>        <th class=\"col_heading level0 col4\" >R 720</th>        <th class=\"col_heading level0 col5\" >R 1800</th>        <th class=\"col_heading level0 col6\" >R 3600</th>        <th class=\"col_heading level0 col7\" >R 7200</th>        <th class=\"col_heading level0 col8\" >R 10800</th>    </tr></thead><tbody>\n",
       "                <tr>\n",
       "                        <th id=\"T_ed444_level0_row0\" class=\"row_heading level0 row0\" >R 0</th>\n",
       "                        <td id=\"T_ed444_row0_col0\" class=\"data row0 col0\" >1.0000</td>\n",
       "                        <td id=\"T_ed444_row0_col1\" class=\"data row0 col1\" >0.9233</td>\n",
       "                        <td id=\"T_ed444_row0_col2\" class=\"data row0 col2\" >0.7183</td>\n",
       "                        <td id=\"T_ed444_row0_col3\" class=\"data row0 col3\" >0.2411</td>\n",
       "                        <td id=\"T_ed444_row0_col4\" class=\"data row0 col4\" >-0.0500</td>\n",
       "                        <td id=\"T_ed444_row0_col5\" class=\"data row0 col5\" >0.0302</td>\n",
       "                        <td id=\"T_ed444_row0_col6\" class=\"data row0 col6\" >0.0256</td>\n",
       "                        <td id=\"T_ed444_row0_col7\" class=\"data row0 col7\" >0.0284</td>\n",
       "                        <td id=\"T_ed444_row0_col8\" class=\"data row0 col8\" >0.0489</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_ed444_level0_row1\" class=\"row_heading level0 row1\" >R 90</th>\n",
       "                        <td id=\"T_ed444_row1_col0\" class=\"data row1 col0\" >0.9233</td>\n",
       "                        <td id=\"T_ed444_row1_col1\" class=\"data row1 col1\" >1.0000</td>\n",
       "                        <td id=\"T_ed444_row1_col2\" class=\"data row1 col2\" >0.9197</td>\n",
       "                        <td id=\"T_ed444_row1_col3\" class=\"data row1 col3\" >0.5137</td>\n",
       "                        <td id=\"T_ed444_row1_col4\" class=\"data row1 col4\" >0.0981</td>\n",
       "                        <td id=\"T_ed444_row1_col5\" class=\"data row1 col5\" >0.0442</td>\n",
       "                        <td id=\"T_ed444_row1_col6\" class=\"data row1 col6\" >0.0441</td>\n",
       "                        <td id=\"T_ed444_row1_col7\" class=\"data row1 col7\" >0.0441</td>\n",
       "                        <td id=\"T_ed444_row1_col8\" class=\"data row1 col8\" >0.0523</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_ed444_level0_row2\" class=\"row_heading level0 row2\" >R 180</th>\n",
       "                        <td id=\"T_ed444_row2_col0\" class=\"data row2 col0\" >0.7183</td>\n",
       "                        <td id=\"T_ed444_row2_col1\" class=\"data row2 col1\" >0.9197</td>\n",
       "                        <td id=\"T_ed444_row2_col2\" class=\"data row2 col2\" >1.0000</td>\n",
       "                        <td id=\"T_ed444_row2_col3\" class=\"data row2 col3\" >0.7901</td>\n",
       "                        <td id=\"T_ed444_row2_col4\" class=\"data row2 col4\" >0.3509</td>\n",
       "                        <td id=\"T_ed444_row2_col5\" class=\"data row2 col5\" >0.1002</td>\n",
       "                        <td id=\"T_ed444_row2_col6\" class=\"data row2 col6\" >0.0579</td>\n",
       "                        <td id=\"T_ed444_row2_col7\" class=\"data row2 col7\" >0.0700</td>\n",
       "                        <td id=\"T_ed444_row2_col8\" class=\"data row2 col8\" >0.0732</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_ed444_level0_row3\" class=\"row_heading level0 row3\" >R 360</th>\n",
       "                        <td id=\"T_ed444_row3_col0\" class=\"data row3 col0\" >0.2411</td>\n",
       "                        <td id=\"T_ed444_row3_col1\" class=\"data row3 col1\" >0.5137</td>\n",
       "                        <td id=\"T_ed444_row3_col2\" class=\"data row3 col2\" >0.7901</td>\n",
       "                        <td id=\"T_ed444_row3_col3\" class=\"data row3 col3\" >1.0000</td>\n",
       "                        <td id=\"T_ed444_row3_col4\" class=\"data row3 col4\" >0.7887</td>\n",
       "                        <td id=\"T_ed444_row3_col5\" class=\"data row3 col5\" >0.2784</td>\n",
       "                        <td id=\"T_ed444_row3_col6\" class=\"data row3 col6\" >0.0751</td>\n",
       "                        <td id=\"T_ed444_row3_col7\" class=\"data row3 col7\" >0.1126</td>\n",
       "                        <td id=\"T_ed444_row3_col8\" class=\"data row3 col8\" >0.1252</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_ed444_level0_row4\" class=\"row_heading level0 row4\" >R 720</th>\n",
       "                        <td id=\"T_ed444_row4_col0\" class=\"data row4 col0\" >-0.0500</td>\n",
       "                        <td id=\"T_ed444_row4_col1\" class=\"data row4 col1\" >0.0981</td>\n",
       "                        <td id=\"T_ed444_row4_col2\" class=\"data row4 col2\" >0.3509</td>\n",
       "                        <td id=\"T_ed444_row4_col3\" class=\"data row4 col3\" >0.7887</td>\n",
       "                        <td id=\"T_ed444_row4_col4\" class=\"data row4 col4\" >1.0000</td>\n",
       "                        <td id=\"T_ed444_row4_col5\" class=\"data row4 col5\" >0.6022</td>\n",
       "                        <td id=\"T_ed444_row4_col6\" class=\"data row4 col6\" >0.1808</td>\n",
       "                        <td id=\"T_ed444_row4_col7\" class=\"data row4 col7\" >0.1729</td>\n",
       "                        <td id=\"T_ed444_row4_col8\" class=\"data row4 col8\" >0.1952</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_ed444_level0_row5\" class=\"row_heading level0 row5\" >R 1800</th>\n",
       "                        <td id=\"T_ed444_row5_col0\" class=\"data row5 col0\" >0.0302</td>\n",
       "                        <td id=\"T_ed444_row5_col1\" class=\"data row5 col1\" >0.0442</td>\n",
       "                        <td id=\"T_ed444_row5_col2\" class=\"data row5 col2\" >0.1002</td>\n",
       "                        <td id=\"T_ed444_row5_col3\" class=\"data row5 col3\" >0.2784</td>\n",
       "                        <td id=\"T_ed444_row5_col4\" class=\"data row5 col4\" >0.6022</td>\n",
       "                        <td id=\"T_ed444_row5_col5\" class=\"data row5 col5\" >1.0000</td>\n",
       "                        <td id=\"T_ed444_row5_col6\" class=\"data row5 col6\" >0.7349</td>\n",
       "                        <td id=\"T_ed444_row5_col7\" class=\"data row5 col7\" >0.4609</td>\n",
       "                        <td id=\"T_ed444_row5_col8\" class=\"data row5 col8\" >0.3664</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_ed444_level0_row6\" class=\"row_heading level0 row6\" >R 3600</th>\n",
       "                        <td id=\"T_ed444_row6_col0\" class=\"data row6 col0\" >0.0256</td>\n",
       "                        <td id=\"T_ed444_row6_col1\" class=\"data row6 col1\" >0.0441</td>\n",
       "                        <td id=\"T_ed444_row6_col2\" class=\"data row6 col2\" >0.0579</td>\n",
       "                        <td id=\"T_ed444_row6_col3\" class=\"data row6 col3\" >0.0751</td>\n",
       "                        <td id=\"T_ed444_row6_col4\" class=\"data row6 col4\" >0.1808</td>\n",
       "                        <td id=\"T_ed444_row6_col5\" class=\"data row6 col5\" >0.7349</td>\n",
       "                        <td id=\"T_ed444_row6_col6\" class=\"data row6 col6\" >1.0000</td>\n",
       "                        <td id=\"T_ed444_row6_col7\" class=\"data row6 col7\" >0.8102</td>\n",
       "                        <td id=\"T_ed444_row6_col8\" class=\"data row6 col8\" >0.6100</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_ed444_level0_row7\" class=\"row_heading level0 row7\" >R 7200</th>\n",
       "                        <td id=\"T_ed444_row7_col0\" class=\"data row7 col0\" >0.0284</td>\n",
       "                        <td id=\"T_ed444_row7_col1\" class=\"data row7 col1\" >0.0441</td>\n",
       "                        <td id=\"T_ed444_row7_col2\" class=\"data row7 col2\" >0.0700</td>\n",
       "                        <td id=\"T_ed444_row7_col3\" class=\"data row7 col3\" >0.1126</td>\n",
       "                        <td id=\"T_ed444_row7_col4\" class=\"data row7 col4\" >0.1729</td>\n",
       "                        <td id=\"T_ed444_row7_col5\" class=\"data row7 col5\" >0.4609</td>\n",
       "                        <td id=\"T_ed444_row7_col6\" class=\"data row7 col6\" >0.8102</td>\n",
       "                        <td id=\"T_ed444_row7_col7\" class=\"data row7 col7\" >1.0000</td>\n",
       "                        <td id=\"T_ed444_row7_col8\" class=\"data row7 col8\" >0.9189</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_ed444_level0_row8\" class=\"row_heading level0 row8\" >R 10800</th>\n",
       "                        <td id=\"T_ed444_row8_col0\" class=\"data row8 col0\" >0.0489</td>\n",
       "                        <td id=\"T_ed444_row8_col1\" class=\"data row8 col1\" >0.0523</td>\n",
       "                        <td id=\"T_ed444_row8_col2\" class=\"data row8 col2\" >0.0732</td>\n",
       "                        <td id=\"T_ed444_row8_col3\" class=\"data row8 col3\" >0.1252</td>\n",
       "                        <td id=\"T_ed444_row8_col4\" class=\"data row8 col4\" >0.1952</td>\n",
       "                        <td id=\"T_ed444_row8_col5\" class=\"data row8 col5\" >0.3664</td>\n",
       "                        <td id=\"T_ed444_row8_col6\" class=\"data row8 col6\" >0.6100</td>\n",
       "                        <td id=\"T_ed444_row8_col7\" class=\"data row8 col7\" >0.9189</td>\n",
       "                        <td id=\"T_ed444_row8_col8\" class=\"data row8 col8\" >1.0000</td>\n",
       "            </tr>\n",
       "    </tbody></table>"
      ],
      "text/plain": [
       "<pandas.io.formats.style.Styler at 0x7ff27db7e460>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "########################################################################\n",
    "# Displaying factors statistics\n",
    "########################################################################\n",
    "\n",
    "table = pd.concat([loadings.min(), loadings.max()], axis=1)\n",
    "table.columns = ['min', 'max']\n",
    "display(table.iloc[:9,:].style.format(\"{:.4f}\").background_gradient(cmap='YlGn'))\n",
    "display(X.iloc[:,:9].corr().style.format(\"{:.4f}\").background_gradient(cmap='YlGn'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.2 Creating Constraints on Key Rate Durations\n",
    "\n",
    "In this example we are going to put a limit on the maximum duration that the portfolio can reach. The key rate durations of portfolio for 1800, 3600 and 7200 days will be lower than -2, -2 and -3."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Disabled</th>\n",
       "      <th>Factor</th>\n",
       "      <th>Sign</th>\n",
       "      <th>Value</th>\n",
       "      <th>Relative Factor</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>False</td>\n",
       "      <td>R 1800</td>\n",
       "      <td>&lt;=</td>\n",
       "      <td>-2</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>False</td>\n",
       "      <td>R 3600</td>\n",
       "      <td>&lt;=</td>\n",
       "      <td>-2</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>False</td>\n",
       "      <td>R 7200</td>\n",
       "      <td>&lt;=</td>\n",
       "      <td>-3</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Disabled  Factor Sign  Value Relative Factor\n",
       "0     False  R 1800   <=     -2                \n",
       "1     False  R 3600   <=     -2                \n",
       "2     False  R 7200   <=     -3                "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "########################################################################\n",
    "# Creating durations constraints\n",
    "########################################################################\n",
    "\n",
    "import riskfolio.ConstraintsFunctions as cf\n",
    "\n",
    "constraints = {'Disabled': [False, False, False],\n",
    "               'Factor': ['R 1800', 'R 3600', 'R 7200'],\n",
    "               'Sign': ['<=', '<=', '<='],\n",
    "               'Value': [-2, -2, -3],\n",
    "               'Relative Factor': ['', '', '']}\n",
    "\n",
    "constraints = pd.DataFrame(constraints)\n",
    "\n",
    "display(constraints)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.3 Estimating Optimum Portfolio with Key Rate Durations Constraints"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style  type=\"text/css\" >\n",
       "#T_59623_row0_col0{\n",
       "            background-color:  #fafdcb;\n",
       "            color:  #000000;\n",
       "        }#T_59623_row1_col0{\n",
       "            background-color:  #ffffe5;\n",
       "            color:  #000000;\n",
       "        }#T_59623_row2_col0{\n",
       "            background-color:  #feffdf;\n",
       "            color:  #000000;\n",
       "        }#T_59623_row3_col0{\n",
       "            background-color:  #b1df90;\n",
       "            color:  #000000;\n",
       "        }#T_59623_row4_col0{\n",
       "            background-color:  #9cd687;\n",
       "            color:  #000000;\n",
       "        }#T_59623_row5_col0{\n",
       "            background-color:  #004529;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_59623_row6_col0{\n",
       "            background-color:  #b9e294;\n",
       "            color:  #000000;\n",
       "        }</style><table id=\"T_59623_\" ><thead>    <tr>        <th class=\"blank level0\" ></th>        <th class=\"col_heading level0 col0\" >weights</th>    </tr></thead><tbody>\n",
       "                <tr>\n",
       "                        <th id=\"T_59623_level0_row0\" class=\"row_heading level0 row0\" >PEP11900D031</th>\n",
       "                        <td id=\"T_59623_row0_col0\" class=\"data row0 col0\" >3.4835%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_59623_level0_row1\" class=\"row_heading level0 row1\" >PEP13000D012</th>\n",
       "                        <td id=\"T_59623_row1_col0\" class=\"data row1 col0\" >0.0000%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_59623_level0_row2\" class=\"row_heading level0 row2\" >PEP13000M088</th>\n",
       "                        <td id=\"T_59623_row2_col0\" class=\"data row2 col0\" >0.8032%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_59623_level0_row3\" class=\"row_heading level0 row3\" >PEP23900M103</th>\n",
       "                        <td id=\"T_59623_row3_col0\" class=\"data row3 col0\" >16.4917%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_59623_level0_row4\" class=\"row_heading level0 row4\" >PEP70101M530</th>\n",
       "                        <td id=\"T_59623_row4_col0\" class=\"data row4 col0\" >18.7616%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_59623_level0_row5\" class=\"row_heading level0 row5\" >PEP70101M571</th>\n",
       "                        <td id=\"T_59623_row5_col0\" class=\"data row5 col0\" >45.1048%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_59623_level0_row6\" class=\"row_heading level0 row6\" >PEP70310M156</th>\n",
       "                        <td id=\"T_59623_row6_col0\" class=\"data row6 col0\" >15.3552%</td>\n",
       "            </tr>\n",
       "    </tbody></table>"
      ],
      "text/plain": [
       "<pandas.io.formats.style.Styler at 0x7ff27db1f580>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "########################################################################\n",
    "# Estimating optimum portfolio with key rate duration constraints\n",
    "########################################################################\n",
    "\n",
    "C, D = cf.factors_constraints(constraints, loadings)\n",
    "\n",
    "port.ainequality = C\n",
    "port.binequality = D\n",
    "\n",
    "w = port.optimization(model=model, rm=rm, obj=obj, rf=rf, l=l, hist=hist)\n",
    "\n",
    "display(w.style.format(\"{:.4%}\").background_gradient(cmap='YlGn'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can see that with this constraints the weights of the portfolio are more spread along all assets. To show that the portfolio full fill all constraints we will calculate the sensitivities of the portfolio."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style  type=\"text/css\" >\n",
       "#T_776bb_row0_col0,#T_776bb_row1_col0,#T_776bb_row2_col0,#T_776bb_row3_col0,#T_776bb_row9_col0,#T_776bb_row10_col0,#T_776bb_row11_col0,#T_776bb_row12_col0{\n",
       "            background-color:  #f7fcbc;\n",
       "            color:  #000000;\n",
       "        }#T_776bb_row4_col0,#T_776bb_row8_col0{\n",
       "            background-color:  #f8fcbe;\n",
       "            color:  #000000;\n",
       "        }#T_776bb_row5_col0{\n",
       "            background-color:  #fcfed4;\n",
       "            color:  #000000;\n",
       "        }#T_776bb_row6_col0{\n",
       "            background-color:  #fcfed6;\n",
       "            color:  #000000;\n",
       "        }#T_776bb_row7_col0{\n",
       "            background-color:  #ffffe5;\n",
       "            color:  #000000;\n",
       "        }#T_776bb_row13_col0{\n",
       "            background-color:  #f6fcb8;\n",
       "            color:  #000000;\n",
       "        }#T_776bb_row14_col0{\n",
       "            background-color:  #c8e99b;\n",
       "            color:  #000000;\n",
       "        }#T_776bb_row15_col0{\n",
       "            background-color:  #9fd788;\n",
       "            color:  #000000;\n",
       "        }#T_776bb_row16_col0{\n",
       "            background-color:  #004529;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_776bb_row17_col0{\n",
       "            background-color:  #f7fcb9;\n",
       "            color:  #000000;\n",
       "        }</style><table id=\"T_776bb_\" ><thead>    <tr>        <th class=\"blank level0\" ></th>        <th class=\"col_heading level0 col0\" >Values</th>    </tr></thead><tbody>\n",
       "                <tr>\n",
       "                        <th id=\"T_776bb_level0_row0\" class=\"row_heading level0 row0\" >R 0</th>\n",
       "                        <td id=\"T_776bb_row0_col0\" class=\"data row0 col0\" >-0.0007</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_776bb_level0_row1\" class=\"row_heading level0 row1\" >R 90</th>\n",
       "                        <td id=\"T_776bb_row1_col0\" class=\"data row1 col0\" >-0.0036</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_776bb_level0_row2\" class=\"row_heading level0 row2\" >R 180</th>\n",
       "                        <td id=\"T_776bb_row2_col0\" class=\"data row2 col0\" >-0.0118</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_776bb_level0_row3\" class=\"row_heading level0 row3\" >R 360</th>\n",
       "                        <td id=\"T_776bb_row3_col0\" class=\"data row3 col0\" >-0.0467</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_776bb_level0_row4\" class=\"row_heading level0 row4\" >R 720</th>\n",
       "                        <td id=\"T_776bb_row4_col0\" class=\"data row4 col0\" >-0.2670</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_776bb_level0_row5\" class=\"row_heading level0 row5\" >R 1800</th>\n",
       "                        <td id=\"T_776bb_row5_col0\" class=\"data row5 col0\" >-2.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_776bb_level0_row6\" class=\"row_heading level0 row6\" >R 3600</th>\n",
       "                        <td id=\"T_776bb_row6_col0\" class=\"data row6 col0\" >-2.1204</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_776bb_level0_row7\" class=\"row_heading level0 row7\" >R 7200</th>\n",
       "                        <td id=\"T_776bb_row7_col0\" class=\"data row7 col0\" >-3.3871</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_776bb_level0_row8\" class=\"row_heading level0 row8\" >R 10800</th>\n",
       "                        <td id=\"T_776bb_row8_col0\" class=\"data row8 col0\" >-0.2030</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_776bb_level0_row9\" class=\"row_heading level0 row9\" >R^2 0</th>\n",
       "                        <td id=\"T_776bb_row9_col0\" class=\"data row9 col0\" >0.0001</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_776bb_level0_row10\" class=\"row_heading level0 row10\" >R^2 90</th>\n",
       "                        <td id=\"T_776bb_row10_col0\" class=\"data row10 col0\" >0.0013</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_776bb_level0_row11\" class=\"row_heading level0 row11\" >R^2 180</th>\n",
       "                        <td id=\"T_776bb_row11_col0\" class=\"data row11 col0\" >0.0055</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_776bb_level0_row12\" class=\"row_heading level0 row12\" >R^2 360</th>\n",
       "                        <td id=\"T_776bb_row12_col0\" class=\"data row12 col0\" >0.0310</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_776bb_level0_row13\" class=\"row_heading level0 row13\" >R^2 720</th>\n",
       "                        <td id=\"T_776bb_row13_col0\" class=\"data row13 col0\" >0.2898</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_776bb_level0_row14\" class=\"row_heading level0 row14\" >R^2 1800</th>\n",
       "                        <td id=\"T_776bb_row14_col0\" class=\"data row14 col0\" >5.0842</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_776bb_level0_row15\" class=\"row_heading level0 row15\" >R^2 3600</th>\n",
       "                        <td id=\"T_776bb_row15_col0\" class=\"data row15 col0\" >8.2361</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_776bb_level0_row16\" class=\"row_heading level0 row16\" >R^2 7200</th>\n",
       "                        <td id=\"T_776bb_row16_col0\" class=\"data row16 col0\" >24.9610</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_776bb_level0_row17\" class=\"row_heading level0 row17\" >R^2 10800</th>\n",
       "                        <td id=\"T_776bb_row17_col0\" class=\"data row17 col0\" >0.2040</td>\n",
       "            </tr>\n",
       "    </tbody></table>"
      ],
      "text/plain": [
       "<pandas.io.formats.style.Styler at 0x7ff2806f7220>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "########################################################################\n",
    "# Calculating portfolio sensitivities for each risk factor\n",
    "########################################################################\n",
    "\n",
    "d_ = np.matrix(loadings).T * np.matrix(w)\n",
    "d_ = pd.DataFrame(d_, index=loadings.columns, columns=['Values'])\n",
    "\n",
    "display(d_.style.format(\"{:.4f}\").background_gradient(cmap='YlGn'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Estimating Mean Variance Portfolio\n",
    "\n",
    "### 4.1 Building the loadings matrix and risk factors returns.\n",
    "\n",
    "This part shows how to build a personalized loadings matrix that will be used by __Riskfolio-Lib__ to calculate the expected returns and covariance matrix."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>APA</th>\n",
       "      <th>CMCSA</th>\n",
       "      <th>CNP</th>\n",
       "      <th>HPQ</th>\n",
       "      <th>PSA</th>\n",
       "      <th>SEE</th>\n",
       "      <th>ZION</th>\n",
       "      <th>R 0</th>\n",
       "      <th>R 90</th>\n",
       "      <th>R 180</th>\n",
       "      <th>...</th>\n",
       "      <th>R 10800</th>\n",
       "      <th>R^2 0</th>\n",
       "      <th>R^2 90</th>\n",
       "      <th>R^2 180</th>\n",
       "      <th>R^2 360</th>\n",
       "      <th>R^2 720</th>\n",
       "      <th>R^2 1800</th>\n",
       "      <th>R^2 3600</th>\n",
       "      <th>R^2 7200</th>\n",
       "      <th>R^2 10800</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2017-11-16</th>\n",
       "      <td>-0.013161</td>\n",
       "      <td>-0.002958</td>\n",
       "      <td>-0.010903</td>\n",
       "      <td>0.009831</td>\n",
       "      <td>0.017234</td>\n",
       "      <td>0.014016</td>\n",
       "      <td>-0.008387</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000059</td>\n",
       "      <td>0.000108</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.000093</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>3.527647e-09</td>\n",
       "      <td>1.163831e-08</td>\n",
       "      <td>3.181621e-08</td>\n",
       "      <td>6.048648e-08</td>\n",
       "      <td>4.552420e-08</td>\n",
       "      <td>5.573518e-09</td>\n",
       "      <td>2.314283e-09</td>\n",
       "      <td>8.695936e-09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-11-15</th>\n",
       "      <td>-0.020296</td>\n",
       "      <td>0.008682</td>\n",
       "      <td>-0.011202</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-0.013479</td>\n",
       "      <td>-0.003326</td>\n",
       "      <td>0.000215</td>\n",
       "      <td>0.00018</td>\n",
       "      <td>0.000247</td>\n",
       "      <td>0.000303</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000417</td>\n",
       "      <td>3.225005e-08</td>\n",
       "      <td>6.082093e-08</td>\n",
       "      <td>9.197391e-08</td>\n",
       "      <td>1.529780e-07</td>\n",
       "      <td>2.448864e-07</td>\n",
       "      <td>3.108575e-07</td>\n",
       "      <td>2.620815e-07</td>\n",
       "      <td>2.025117e-07</td>\n",
       "      <td>1.736147e-07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-11-14</th>\n",
       "      <td>-0.037020</td>\n",
       "      <td>-0.010470</td>\n",
       "      <td>0.010800</td>\n",
       "      <td>0.008975</td>\n",
       "      <td>-0.001548</td>\n",
       "      <td>0.002668</td>\n",
       "      <td>0.026950</td>\n",
       "      <td>-0.00180</td>\n",
       "      <td>-0.001710</td>\n",
       "      <td>-0.001624</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000861</td>\n",
       "      <td>3.241235e-06</td>\n",
       "      <td>2.925701e-06</td>\n",
       "      <td>2.636999e-06</td>\n",
       "      <td>2.131988e-06</td>\n",
       "      <td>1.362113e-06</td>\n",
       "      <td>2.555747e-07</td>\n",
       "      <td>1.966922e-08</td>\n",
       "      <td>4.572289e-07</td>\n",
       "      <td>7.417774e-07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-11-13</th>\n",
       "      <td>-0.014503</td>\n",
       "      <td>0.010855</td>\n",
       "      <td>0.007480</td>\n",
       "      <td>-0.002826</td>\n",
       "      <td>0.008179</td>\n",
       "      <td>0.014205</td>\n",
       "      <td>0.034145</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000013</td>\n",
       "      <td>0.000025</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000364</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.681950e-10</td>\n",
       "      <td>6.400900e-10</td>\n",
       "      <td>2.324301e-09</td>\n",
       "      <td>7.748577e-09</td>\n",
       "      <td>3.033345e-08</td>\n",
       "      <td>6.657174e-08</td>\n",
       "      <td>1.116368e-07</td>\n",
       "      <td>1.328391e-07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-11-10</th>\n",
       "      <td>-0.024536</td>\n",
       "      <td>0.007932</td>\n",
       "      <td>-0.013418</td>\n",
       "      <td>-0.005155</td>\n",
       "      <td>0.000710</td>\n",
       "      <td>-0.009381</td>\n",
       "      <td>-0.000910</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000026</td>\n",
       "      <td>0.000043</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.001054</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>6.770924e-10</td>\n",
       "      <td>1.820644e-09</td>\n",
       "      <td>2.881542e-09</td>\n",
       "      <td>2.791573e-10</td>\n",
       "      <td>6.131368e-08</td>\n",
       "      <td>3.779975e-07</td>\n",
       "      <td>8.759856e-07</td>\n",
       "      <td>1.110697e-06</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 25 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                 APA     CMCSA       CNP       HPQ       PSA       SEE  \\\n",
       "Date                                                                     \n",
       "2017-11-16 -0.013161 -0.002958 -0.010903  0.009831  0.017234  0.014016   \n",
       "2017-11-15 -0.020296  0.008682 -0.011202  0.000000 -0.013479 -0.003326   \n",
       "2017-11-14 -0.037020 -0.010470  0.010800  0.008975 -0.001548  0.002668   \n",
       "2017-11-13 -0.014503  0.010855  0.007480 -0.002826  0.008179  0.014205   \n",
       "2017-11-10 -0.024536  0.007932 -0.013418 -0.005155  0.000710 -0.009381   \n",
       "\n",
       "                ZION      R 0      R 90     R 180  ...   R 10800  \\\n",
       "Date                                               ...             \n",
       "2017-11-16 -0.008387  0.00000  0.000059  0.000108  ... -0.000093   \n",
       "2017-11-15  0.000215  0.00018  0.000247  0.000303  ...  0.000417   \n",
       "2017-11-14  0.026950 -0.00180 -0.001710 -0.001624  ...  0.000861   \n",
       "2017-11-13  0.034145  0.00000  0.000013  0.000025  ...  0.000364   \n",
       "2017-11-10 -0.000910  0.00000  0.000026  0.000043  ... -0.001054   \n",
       "\n",
       "                   R^2 0        R^2 90       R^2 180       R^2 360  \\\n",
       "Date                                                                 \n",
       "2017-11-16  0.000000e+00  3.527647e-09  1.163831e-08  3.181621e-08   \n",
       "2017-11-15  3.225005e-08  6.082093e-08  9.197391e-08  1.529780e-07   \n",
       "2017-11-14  3.241235e-06  2.925701e-06  2.636999e-06  2.131988e-06   \n",
       "2017-11-13  0.000000e+00  1.681950e-10  6.400900e-10  2.324301e-09   \n",
       "2017-11-10  0.000000e+00  6.770924e-10  1.820644e-09  2.881542e-09   \n",
       "\n",
       "                 R^2 720      R^2 1800      R^2 3600      R^2 7200  \\\n",
       "Date                                                                 \n",
       "2017-11-16  6.048648e-08  4.552420e-08  5.573518e-09  2.314283e-09   \n",
       "2017-11-15  2.448864e-07  3.108575e-07  2.620815e-07  2.025117e-07   \n",
       "2017-11-14  1.362113e-06  2.555747e-07  1.966922e-08  4.572289e-07   \n",
       "2017-11-13  7.748577e-09  3.033345e-08  6.657174e-08  1.116368e-07   \n",
       "2017-11-10  2.791573e-10  6.131368e-08  3.779975e-07  8.759856e-07   \n",
       "\n",
       "               R^2 10800  \n",
       "Date                      \n",
       "2017-11-16  8.695936e-09  \n",
       "2017-11-15  1.736147e-07  \n",
       "2017-11-14  7.417774e-07  \n",
       "2017-11-13  1.328391e-07  \n",
       "2017-11-10  1.110697e-06  \n",
       "\n",
       "[5 rows x 25 columns]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "########################################################################\n",
    "# Building the risk factors returns matrix\n",
    "########################################################################\n",
    "\n",
    "# Removing bond returns from factors matrix\n",
    "cols = assets_returns.columns\n",
    "cols = ~cols.isin(loadings.index)\n",
    "cols = assets_returns.columns[cols]\n",
    "\n",
    "# Other approach for removing bond returns from factors matrix\n",
    "cols = [col for col in assets_returns.columns if col not in loadings.index]\n",
    "\n",
    "X = pd.concat([assets_returns[cols], X], axis=1)\n",
    "\n",
    "display(X.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>APA</th>\n",
       "      <th>CMCSA</th>\n",
       "      <th>CNP</th>\n",
       "      <th>HPQ</th>\n",
       "      <th>PSA</th>\n",
       "      <th>SEE</th>\n",
       "      <th>ZION</th>\n",
       "      <th>PEP11900D031</th>\n",
       "      <th>PEP13000D012</th>\n",
       "      <th>PEP13000M088</th>\n",
       "      <th>PEP23900M103</th>\n",
       "      <th>PEP70101M530</th>\n",
       "      <th>PEP70101M571</th>\n",
       "      <th>PEP70310M156</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2017-11-16</th>\n",
       "      <td>-0.013161</td>\n",
       "      <td>-0.002958</td>\n",
       "      <td>-0.010903</td>\n",
       "      <td>0.009831</td>\n",
       "      <td>0.017234</td>\n",
       "      <td>0.014016</td>\n",
       "      <td>-0.008387</td>\n",
       "      <td>-0.000411</td>\n",
       "      <td>-0.000380</td>\n",
       "      <td>-0.000597</td>\n",
       "      <td>-0.000737</td>\n",
       "      <td>-0.000116</td>\n",
       "      <td>0.000076</td>\n",
       "      <td>-0.000633</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-11-15</th>\n",
       "      <td>-0.020296</td>\n",
       "      <td>0.008682</td>\n",
       "      <td>-0.011202</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-0.013479</td>\n",
       "      <td>-0.003326</td>\n",
       "      <td>0.000215</td>\n",
       "      <td>-0.001626</td>\n",
       "      <td>-0.003076</td>\n",
       "      <td>-0.003041</td>\n",
       "      <td>-0.002286</td>\n",
       "      <td>-0.004459</td>\n",
       "      <td>-0.004651</td>\n",
       "      <td>-0.002146</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-11-14</th>\n",
       "      <td>-0.037020</td>\n",
       "      <td>-0.010470</td>\n",
       "      <td>0.010800</td>\n",
       "      <td>0.008975</td>\n",
       "      <td>-0.001548</td>\n",
       "      <td>0.002668</td>\n",
       "      <td>0.026950</td>\n",
       "      <td>0.002320</td>\n",
       "      <td>0.000236</td>\n",
       "      <td>0.001040</td>\n",
       "      <td>0.002373</td>\n",
       "      <td>-0.002741</td>\n",
       "      <td>-0.003932</td>\n",
       "      <td>0.002151</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-11-13</th>\n",
       "      <td>-0.014503</td>\n",
       "      <td>0.010855</td>\n",
       "      <td>0.007480</td>\n",
       "      <td>-0.002826</td>\n",
       "      <td>0.008179</td>\n",
       "      <td>0.014205</td>\n",
       "      <td>0.034145</td>\n",
       "      <td>0.000906</td>\n",
       "      <td>0.000064</td>\n",
       "      <td>-0.000767</td>\n",
       "      <td>0.000354</td>\n",
       "      <td>-0.000835</td>\n",
       "      <td>-0.001114</td>\n",
       "      <td>-0.000081</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-11-10</th>\n",
       "      <td>-0.024536</td>\n",
       "      <td>0.007932</td>\n",
       "      <td>-0.013418</td>\n",
       "      <td>-0.005155</td>\n",
       "      <td>0.000710</td>\n",
       "      <td>-0.009381</td>\n",
       "      <td>-0.000910</td>\n",
       "      <td>0.001194</td>\n",
       "      <td>0.003792</td>\n",
       "      <td>0.003047</td>\n",
       "      <td>0.001355</td>\n",
       "      <td>0.007118</td>\n",
       "      <td>0.008207</td>\n",
       "      <td>-0.000090</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 APA     CMCSA       CNP       HPQ       PSA       SEE  \\\n",
       "Date                                                                     \n",
       "2017-11-16 -0.013161 -0.002958 -0.010903  0.009831  0.017234  0.014016   \n",
       "2017-11-15 -0.020296  0.008682 -0.011202  0.000000 -0.013479 -0.003326   \n",
       "2017-11-14 -0.037020 -0.010470  0.010800  0.008975 -0.001548  0.002668   \n",
       "2017-11-13 -0.014503  0.010855  0.007480 -0.002826  0.008179  0.014205   \n",
       "2017-11-10 -0.024536  0.007932 -0.013418 -0.005155  0.000710 -0.009381   \n",
       "\n",
       "                ZION  PEP11900D031  PEP13000D012  PEP13000M088  PEP23900M103  \\\n",
       "Date                                                                           \n",
       "2017-11-16 -0.008387     -0.000411     -0.000380     -0.000597     -0.000737   \n",
       "2017-11-15  0.000215     -0.001626     -0.003076     -0.003041     -0.002286   \n",
       "2017-11-14  0.026950      0.002320      0.000236      0.001040      0.002373   \n",
       "2017-11-13  0.034145      0.000906      0.000064     -0.000767      0.000354   \n",
       "2017-11-10 -0.000910      0.001194      0.003792      0.003047      0.001355   \n",
       "\n",
       "            PEP70101M530  PEP70101M571  PEP70310M156  \n",
       "Date                                                  \n",
       "2017-11-16     -0.000116      0.000076     -0.000633  \n",
       "2017-11-15     -0.004459     -0.004651     -0.002146  \n",
       "2017-11-14     -0.002741     -0.003932      0.002151  \n",
       "2017-11-13     -0.000835     -0.001114     -0.000081  \n",
       "2017-11-10      0.007118      0.008207     -0.000090  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "########################################################################\n",
    "# Building the asset returns matrix\n",
    "########################################################################\n",
    "\n",
    "Y = pd.concat([assets_returns[cols], Y], axis=1)\n",
    "\n",
    "display(Y.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style  type=\"text/css\" >\n",
       "#T_51c51_row0_col0,#T_51c51_row0_col7,#T_51c51_row0_col8,#T_51c51_row0_col9,#T_51c51_row0_col10,#T_51c51_row0_col11,#T_51c51_row0_col12,#T_51c51_row0_col13,#T_51c51_row0_col14,#T_51c51_row0_col15,#T_51c51_row1_col1,#T_51c51_row1_col7,#T_51c51_row1_col8,#T_51c51_row1_col9,#T_51c51_row1_col10,#T_51c51_row1_col11,#T_51c51_row1_col12,#T_51c51_row1_col13,#T_51c51_row1_col14,#T_51c51_row1_col15,#T_51c51_row2_col2,#T_51c51_row2_col7,#T_51c51_row2_col8,#T_51c51_row2_col9,#T_51c51_row2_col10,#T_51c51_row2_col11,#T_51c51_row2_col12,#T_51c51_row2_col13,#T_51c51_row2_col14,#T_51c51_row2_col15,#T_51c51_row3_col3,#T_51c51_row3_col7,#T_51c51_row3_col8,#T_51c51_row3_col9,#T_51c51_row3_col10,#T_51c51_row3_col11,#T_51c51_row3_col12,#T_51c51_row3_col13,#T_51c51_row3_col14,#T_51c51_row3_col15,#T_51c51_row4_col4,#T_51c51_row4_col7,#T_51c51_row4_col8,#T_51c51_row4_col9,#T_51c51_row4_col10,#T_51c51_row4_col11,#T_51c51_row4_col12,#T_51c51_row4_col13,#T_51c51_row4_col14,#T_51c51_row4_col15,#T_51c51_row5_col5,#T_51c51_row5_col7,#T_51c51_row5_col8,#T_51c51_row5_col9,#T_51c51_row5_col10,#T_51c51_row5_col11,#T_51c51_row5_col12,#T_51c51_row5_col13,#T_51c51_row5_col14,#T_51c51_row5_col15,#T_51c51_row6_col6,#T_51c51_row6_col7,#T_51c51_row6_col8,#T_51c51_row6_col9,#T_51c51_row6_col10,#T_51c51_row6_col11,#T_51c51_row6_col12,#T_51c51_row6_col13,#T_51c51_row6_col14,#T_51c51_row6_col15,#T_51c51_row7_col13,#T_51c51_row7_col14,#T_51c51_row7_col15,#T_51c51_row7_col19,#T_51c51_row7_col20,#T_51c51_row8_col7,#T_51c51_row8_col15,#T_51c51_row8_col17,#T_51c51_row8_col22,#T_51c51_row9_col14,#T_51c51_row9_col15,#T_51c51_row9_col16,#T_51c51_row9_col18,#T_51c51_row10_col7,#T_51c51_row10_col14,#T_51c51_row10_col15,#T_51c51_row10_col21,#T_51c51_row11_col7,#T_51c51_row11_col15,#T_51c51_row12_col23,#T_51c51_row12_col24,#T_51c51_row13_col7,#T_51c51_row13_col13,#T_51c51_row13_col14,#T_51c51_row13_col15{\n",
       "            background-color:  #004529;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_51c51_row0_col1,#T_51c51_row0_col2,#T_51c51_row0_col3,#T_51c51_row0_col4,#T_51c51_row0_col5,#T_51c51_row0_col6,#T_51c51_row0_col16,#T_51c51_row0_col17,#T_51c51_row0_col18,#T_51c51_row0_col19,#T_51c51_row0_col20,#T_51c51_row0_col21,#T_51c51_row0_col22,#T_51c51_row0_col23,#T_51c51_row0_col24,#T_51c51_row1_col0,#T_51c51_row1_col2,#T_51c51_row1_col3,#T_51c51_row1_col4,#T_51c51_row1_col5,#T_51c51_row1_col6,#T_51c51_row1_col16,#T_51c51_row1_col17,#T_51c51_row1_col18,#T_51c51_row1_col19,#T_51c51_row1_col20,#T_51c51_row1_col21,#T_51c51_row1_col22,#T_51c51_row1_col23,#T_51c51_row1_col24,#T_51c51_row2_col0,#T_51c51_row2_col1,#T_51c51_row2_col3,#T_51c51_row2_col4,#T_51c51_row2_col5,#T_51c51_row2_col6,#T_51c51_row2_col16,#T_51c51_row2_col17,#T_51c51_row2_col18,#T_51c51_row2_col19,#T_51c51_row2_col20,#T_51c51_row2_col21,#T_51c51_row2_col22,#T_51c51_row2_col23,#T_51c51_row2_col24,#T_51c51_row3_col0,#T_51c51_row3_col1,#T_51c51_row3_col2,#T_51c51_row3_col4,#T_51c51_row3_col5,#T_51c51_row3_col6,#T_51c51_row3_col16,#T_51c51_row3_col17,#T_51c51_row3_col18,#T_51c51_row3_col19,#T_51c51_row3_col20,#T_51c51_row3_col21,#T_51c51_row3_col22,#T_51c51_row3_col23,#T_51c51_row3_col24,#T_51c51_row4_col0,#T_51c51_row4_col1,#T_51c51_row4_col2,#T_51c51_row4_col3,#T_51c51_row4_col5,#T_51c51_row4_col6,#T_51c51_row4_col16,#T_51c51_row4_col17,#T_51c51_row4_col18,#T_51c51_row4_col19,#T_51c51_row4_col20,#T_51c51_row4_col21,#T_51c51_row4_col22,#T_51c51_row4_col23,#T_51c51_row4_col24,#T_51c51_row5_col0,#T_51c51_row5_col1,#T_51c51_row5_col2,#T_51c51_row5_col3,#T_51c51_row5_col4,#T_51c51_row5_col6,#T_51c51_row5_col16,#T_51c51_row5_col17,#T_51c51_row5_col18,#T_51c51_row5_col19,#T_51c51_row5_col20,#T_51c51_row5_col21,#T_51c51_row5_col22,#T_51c51_row5_col23,#T_51c51_row5_col24,#T_51c51_row6_col0,#T_51c51_row6_col1,#T_51c51_row6_col2,#T_51c51_row6_col3,#T_51c51_row6_col4,#T_51c51_row6_col5,#T_51c51_row6_col16,#T_51c51_row6_col17,#T_51c51_row6_col18,#T_51c51_row6_col19,#T_51c51_row6_col20,#T_51c51_row6_col21,#T_51c51_row6_col22,#T_51c51_row6_col23,#T_51c51_row6_col24,#T_51c51_row7_col0,#T_51c51_row7_col1,#T_51c51_row7_col2,#T_51c51_row7_col3,#T_51c51_row7_col4,#T_51c51_row7_col5,#T_51c51_row7_col6,#T_51c51_row7_col9,#T_51c51_row7_col10,#T_51c51_row7_col11,#T_51c51_row7_col22,#T_51c51_row7_col23,#T_51c51_row7_col24,#T_51c51_row8_col0,#T_51c51_row8_col1,#T_51c51_row8_col2,#T_51c51_row8_col3,#T_51c51_row8_col4,#T_51c51_row8_col5,#T_51c51_row8_col6,#T_51c51_row8_col8,#T_51c51_row8_col16,#T_51c51_row8_col23,#T_51c51_row8_col24,#T_51c51_row9_col0,#T_51c51_row9_col1,#T_51c51_row9_col2,#T_51c51_row9_col3,#T_51c51_row9_col4,#T_51c51_row9_col5,#T_51c51_row9_col6,#T_51c51_row9_col23,#T_51c51_row9_col24,#T_51c51_row10_col0,#T_51c51_row10_col1,#T_51c51_row10_col2,#T_51c51_row10_col3,#T_51c51_row10_col4,#T_51c51_row10_col5,#T_51c51_row10_col6,#T_51c51_row10_col12,#T_51c51_row10_col16,#T_51c51_row10_col22,#T_51c51_row10_col23,#T_51c51_row10_col24,#T_51c51_row11_col0,#T_51c51_row11_col1,#T_51c51_row11_col2,#T_51c51_row11_col3,#T_51c51_row11_col4,#T_51c51_row11_col5,#T_51c51_row11_col6,#T_51c51_row11_col13,#T_51c51_row11_col16,#T_51c51_row11_col24,#T_51c51_row12_col0,#T_51c51_row12_col1,#T_51c51_row12_col2,#T_51c51_row12_col3,#T_51c51_row12_col4,#T_51c51_row12_col5,#T_51c51_row12_col6,#T_51c51_row12_col7,#T_51c51_row12_col14,#T_51c51_row12_col15,#T_51c51_row13_col0,#T_51c51_row13_col1,#T_51c51_row13_col2,#T_51c51_row13_col3,#T_51c51_row13_col4,#T_51c51_row13_col5,#T_51c51_row13_col6,#T_51c51_row13_col16,#T_51c51_row13_col22,#T_51c51_row13_col23,#T_51c51_row13_col24{\n",
       "            background-color:  #ffffe5;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row7_col7{\n",
       "            background-color:  #e2f4aa;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row7_col8,#T_51c51_row11_col18{\n",
       "            background-color:  #d2eda0;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row7_col12,#T_51c51_row12_col11{\n",
       "            background-color:  #ddf2a6;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row7_col16{\n",
       "            background-color:  #b9e294;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row7_col17,#T_51c51_row12_col17{\n",
       "            background-color:  #5cb86b;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row7_col18{\n",
       "            background-color:  #016937;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_51c51_row7_col21{\n",
       "            background-color:  #2e924c;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row8_col9,#T_51c51_row11_col23{\n",
       "            background-color:  #d5eea1;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row8_col10{\n",
       "            background-color:  #f0f9b4;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row8_col11{\n",
       "            background-color:  #f9fdc4;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row8_col12{\n",
       "            background-color:  #298c48;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row8_col13,#T_51c51_row13_col12{\n",
       "            background-color:  #feffde;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row8_col14,#T_51c51_row13_col21{\n",
       "            background-color:  #004e2d;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_51c51_row8_col18,#T_51c51_row13_col17{\n",
       "            background-color:  #b1df90;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row8_col19{\n",
       "            background-color:  #11753d;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_51c51_row8_col20,#T_51c51_row10_col8{\n",
       "            background-color:  #005830;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_51c51_row8_col21{\n",
       "            background-color:  #e4f4ab;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row9_col7{\n",
       "            background-color:  #f7fcb9;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row9_col8{\n",
       "            background-color:  #00522e;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_51c51_row9_col9{\n",
       "            background-color:  #ddf1a6;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row9_col10,#T_51c51_row12_col10{\n",
       "            background-color:  #c3e698;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row9_col11{\n",
       "            background-color:  #daf0a4;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row9_col12,#T_51c51_row10_col11{\n",
       "            background-color:  #abdc8d;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row9_col13{\n",
       "            background-color:  #aedd8e;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row9_col17{\n",
       "            background-color:  #feffe1;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row9_col19,#T_51c51_row9_col20{\n",
       "            background-color:  #167a3f;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_51c51_row9_col21{\n",
       "            background-color:  #a2d88a;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row9_col22{\n",
       "            background-color:  #b2df90;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row10_col9{\n",
       "            background-color:  #a7db8c;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row10_col10{\n",
       "            background-color:  #90d083;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row10_col13{\n",
       "            background-color:  #004629;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_51c51_row10_col17{\n",
       "            background-color:  #ffffe4;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row10_col18{\n",
       "            background-color:  #12763d;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_51c51_row10_col19{\n",
       "            background-color:  #30954f;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row10_col20,#T_51c51_row12_col18{\n",
       "            background-color:  #379e54;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row11_col8,#T_51c51_row11_col11{\n",
       "            background-color:  #bee596;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row11_col9,#T_51c51_row12_col13{\n",
       "            background-color:  #83cb7d;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row11_col10{\n",
       "            background-color:  #a4d98a;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row11_col12{\n",
       "            background-color:  #1a7d40;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_51c51_row11_col14{\n",
       "            background-color:  #9dd688;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row11_col17{\n",
       "            background-color:  #39a056;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row11_col19{\n",
       "            background-color:  #4ab062;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row11_col20{\n",
       "            background-color:  #2c8f4b;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row11_col21{\n",
       "            background-color:  #edf8b1;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row11_col22{\n",
       "            background-color:  #086e3a;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_51c51_row12_col8,#T_51c51_row13_col9{\n",
       "            background-color:  #79c679;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row12_col9{\n",
       "            background-color:  #b8e293;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row12_col12{\n",
       "            background-color:  #248644;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row12_col16{\n",
       "            background-color:  #89ce80;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row12_col19{\n",
       "            background-color:  #31974f;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row12_col20{\n",
       "            background-color:  #13773d;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_51c51_row12_col21{\n",
       "            background-color:  #e7f6ad;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row12_col22{\n",
       "            background-color:  #8dcf81;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row13_col8{\n",
       "            background-color:  #77c679;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row13_col10{\n",
       "            background-color:  #8ed082;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row13_col11{\n",
       "            background-color:  #cbea9c;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row13_col18{\n",
       "            background-color:  #c4e799;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row13_col19{\n",
       "            background-color:  #56b568;\n",
       "            color:  #000000;\n",
       "        }#T_51c51_row13_col20{\n",
       "            background-color:  #3ca458;\n",
       "            color:  #000000;\n",
       "        }</style><table id=\"T_51c51_\" ><thead>    <tr>        <th class=\"blank level0\" ></th>        <th class=\"col_heading level0 col0\" >APA</th>        <th class=\"col_heading level0 col1\" >CMCSA</th>        <th class=\"col_heading level0 col2\" >CNP</th>        <th class=\"col_heading level0 col3\" >HPQ</th>        <th class=\"col_heading level0 col4\" >PSA</th>        <th class=\"col_heading level0 col5\" >SEE</th>        <th class=\"col_heading level0 col6\" >ZION</th>        <th class=\"col_heading level0 col7\" >R 0</th>        <th class=\"col_heading level0 col8\" >R 90</th>        <th class=\"col_heading level0 col9\" >R 180</th>        <th class=\"col_heading level0 col10\" >R 360</th>        <th class=\"col_heading level0 col11\" >R 720</th>        <th class=\"col_heading level0 col12\" >R 1800</th>        <th class=\"col_heading level0 col13\" >R 3600</th>        <th class=\"col_heading level0 col14\" >R 7200</th>        <th class=\"col_heading level0 col15\" >R 10800</th>        <th class=\"col_heading level0 col16\" >R^2 0</th>        <th class=\"col_heading level0 col17\" >R^2 90</th>        <th class=\"col_heading level0 col18\" >R^2 180</th>        <th class=\"col_heading level0 col19\" >R^2 360</th>        <th class=\"col_heading level0 col20\" >R^2 720</th>        <th class=\"col_heading level0 col21\" >R^2 1800</th>        <th class=\"col_heading level0 col22\" >R^2 3600</th>        <th class=\"col_heading level0 col23\" >R^2 7200</th>        <th class=\"col_heading level0 col24\" >R^2 10800</th>    </tr></thead><tbody>\n",
       "                <tr>\n",
       "                        <th id=\"T_51c51_level0_row0\" class=\"row_heading level0 row0\" >APA</th>\n",
       "                        <td id=\"T_51c51_row0_col0\" class=\"data row0 col0\" >1.0000</td>\n",
       "                        <td id=\"T_51c51_row0_col1\" class=\"data row0 col1\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row0_col2\" class=\"data row0 col2\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row0_col3\" class=\"data row0 col3\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row0_col4\" class=\"data row0 col4\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row0_col5\" class=\"data row0 col5\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row0_col6\" class=\"data row0 col6\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row0_col7\" class=\"data row0 col7\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row0_col8\" class=\"data row0 col8\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row0_col9\" class=\"data row0 col9\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row0_col10\" class=\"data row0 col10\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row0_col11\" class=\"data row0 col11\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row0_col12\" class=\"data row0 col12\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row0_col13\" class=\"data row0 col13\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row0_col14\" class=\"data row0 col14\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row0_col15\" class=\"data row0 col15\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row0_col16\" class=\"data row0 col16\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row0_col17\" class=\"data row0 col17\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row0_col18\" class=\"data row0 col18\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row0_col19\" class=\"data row0 col19\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row0_col20\" class=\"data row0 col20\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row0_col21\" class=\"data row0 col21\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row0_col22\" class=\"data row0 col22\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row0_col23\" class=\"data row0 col23\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row0_col24\" class=\"data row0 col24\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_51c51_level0_row1\" class=\"row_heading level0 row1\" >CMCSA</th>\n",
       "                        <td id=\"T_51c51_row1_col0\" class=\"data row1 col0\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row1_col1\" class=\"data row1 col1\" >1.0000</td>\n",
       "                        <td id=\"T_51c51_row1_col2\" class=\"data row1 col2\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row1_col3\" class=\"data row1 col3\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row1_col4\" class=\"data row1 col4\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row1_col5\" class=\"data row1 col5\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row1_col6\" class=\"data row1 col6\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row1_col7\" class=\"data row1 col7\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row1_col8\" class=\"data row1 col8\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row1_col9\" class=\"data row1 col9\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row1_col10\" class=\"data row1 col10\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row1_col11\" class=\"data row1 col11\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row1_col12\" class=\"data row1 col12\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row1_col13\" class=\"data row1 col13\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row1_col14\" class=\"data row1 col14\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row1_col15\" class=\"data row1 col15\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row1_col16\" class=\"data row1 col16\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row1_col17\" class=\"data row1 col17\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row1_col18\" class=\"data row1 col18\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row1_col19\" class=\"data row1 col19\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row1_col20\" class=\"data row1 col20\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row1_col21\" class=\"data row1 col21\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row1_col22\" class=\"data row1 col22\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row1_col23\" class=\"data row1 col23\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row1_col24\" class=\"data row1 col24\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_51c51_level0_row2\" class=\"row_heading level0 row2\" >CNP</th>\n",
       "                        <td id=\"T_51c51_row2_col0\" class=\"data row2 col0\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row2_col1\" class=\"data row2 col1\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row2_col2\" class=\"data row2 col2\" >1.0000</td>\n",
       "                        <td id=\"T_51c51_row2_col3\" class=\"data row2 col3\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row2_col4\" class=\"data row2 col4\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row2_col5\" class=\"data row2 col5\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row2_col6\" class=\"data row2 col6\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row2_col7\" class=\"data row2 col7\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row2_col8\" class=\"data row2 col8\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row2_col9\" class=\"data row2 col9\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row2_col10\" class=\"data row2 col10\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row2_col11\" class=\"data row2 col11\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row2_col12\" class=\"data row2 col12\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row2_col13\" class=\"data row2 col13\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row2_col14\" class=\"data row2 col14\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row2_col15\" class=\"data row2 col15\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row2_col16\" class=\"data row2 col16\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row2_col17\" class=\"data row2 col17\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row2_col18\" class=\"data row2 col18\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row2_col19\" class=\"data row2 col19\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row2_col20\" class=\"data row2 col20\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row2_col21\" class=\"data row2 col21\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row2_col22\" class=\"data row2 col22\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row2_col23\" class=\"data row2 col23\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row2_col24\" class=\"data row2 col24\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_51c51_level0_row3\" class=\"row_heading level0 row3\" >HPQ</th>\n",
       "                        <td id=\"T_51c51_row3_col0\" class=\"data row3 col0\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row3_col1\" class=\"data row3 col1\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row3_col2\" class=\"data row3 col2\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row3_col3\" class=\"data row3 col3\" >1.0000</td>\n",
       "                        <td id=\"T_51c51_row3_col4\" class=\"data row3 col4\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row3_col5\" class=\"data row3 col5\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row3_col6\" class=\"data row3 col6\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row3_col7\" class=\"data row3 col7\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row3_col8\" class=\"data row3 col8\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row3_col9\" class=\"data row3 col9\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row3_col10\" class=\"data row3 col10\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row3_col11\" class=\"data row3 col11\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row3_col12\" class=\"data row3 col12\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row3_col13\" class=\"data row3 col13\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row3_col14\" class=\"data row3 col14\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row3_col15\" class=\"data row3 col15\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row3_col16\" class=\"data row3 col16\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row3_col17\" class=\"data row3 col17\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row3_col18\" class=\"data row3 col18\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row3_col19\" class=\"data row3 col19\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row3_col20\" class=\"data row3 col20\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row3_col21\" class=\"data row3 col21\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row3_col22\" class=\"data row3 col22\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row3_col23\" class=\"data row3 col23\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row3_col24\" class=\"data row3 col24\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_51c51_level0_row4\" class=\"row_heading level0 row4\" >PSA</th>\n",
       "                        <td id=\"T_51c51_row4_col0\" class=\"data row4 col0\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row4_col1\" class=\"data row4 col1\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row4_col2\" class=\"data row4 col2\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row4_col3\" class=\"data row4 col3\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row4_col4\" class=\"data row4 col4\" >1.0000</td>\n",
       "                        <td id=\"T_51c51_row4_col5\" class=\"data row4 col5\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row4_col6\" class=\"data row4 col6\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row4_col7\" class=\"data row4 col7\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row4_col8\" class=\"data row4 col8\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row4_col9\" class=\"data row4 col9\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row4_col10\" class=\"data row4 col10\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row4_col11\" class=\"data row4 col11\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row4_col12\" class=\"data row4 col12\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row4_col13\" class=\"data row4 col13\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row4_col14\" class=\"data row4 col14\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row4_col15\" class=\"data row4 col15\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row4_col16\" class=\"data row4 col16\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row4_col17\" class=\"data row4 col17\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row4_col18\" class=\"data row4 col18\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row4_col19\" class=\"data row4 col19\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row4_col20\" class=\"data row4 col20\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row4_col21\" class=\"data row4 col21\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row4_col22\" class=\"data row4 col22\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row4_col23\" class=\"data row4 col23\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row4_col24\" class=\"data row4 col24\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_51c51_level0_row5\" class=\"row_heading level0 row5\" >SEE</th>\n",
       "                        <td id=\"T_51c51_row5_col0\" class=\"data row5 col0\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row5_col1\" class=\"data row5 col1\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row5_col2\" class=\"data row5 col2\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row5_col3\" class=\"data row5 col3\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row5_col4\" class=\"data row5 col4\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row5_col5\" class=\"data row5 col5\" >1.0000</td>\n",
       "                        <td id=\"T_51c51_row5_col6\" class=\"data row5 col6\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row5_col7\" class=\"data row5 col7\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row5_col8\" class=\"data row5 col8\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row5_col9\" class=\"data row5 col9\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row5_col10\" class=\"data row5 col10\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row5_col11\" class=\"data row5 col11\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row5_col12\" class=\"data row5 col12\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row5_col13\" class=\"data row5 col13\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row5_col14\" class=\"data row5 col14\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row5_col15\" class=\"data row5 col15\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row5_col16\" class=\"data row5 col16\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row5_col17\" class=\"data row5 col17\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row5_col18\" class=\"data row5 col18\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row5_col19\" class=\"data row5 col19\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row5_col20\" class=\"data row5 col20\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row5_col21\" class=\"data row5 col21\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row5_col22\" class=\"data row5 col22\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row5_col23\" class=\"data row5 col23\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row5_col24\" class=\"data row5 col24\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_51c51_level0_row6\" class=\"row_heading level0 row6\" >ZION</th>\n",
       "                        <td id=\"T_51c51_row6_col0\" class=\"data row6 col0\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row6_col1\" class=\"data row6 col1\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row6_col2\" class=\"data row6 col2\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row6_col3\" class=\"data row6 col3\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row6_col4\" class=\"data row6 col4\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row6_col5\" class=\"data row6 col5\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row6_col6\" class=\"data row6 col6\" >1.0000</td>\n",
       "                        <td id=\"T_51c51_row6_col7\" class=\"data row6 col7\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row6_col8\" class=\"data row6 col8\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row6_col9\" class=\"data row6 col9\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row6_col10\" class=\"data row6 col10\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row6_col11\" class=\"data row6 col11\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row6_col12\" class=\"data row6 col12\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row6_col13\" class=\"data row6 col13\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row6_col14\" class=\"data row6 col14\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row6_col15\" class=\"data row6 col15\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row6_col16\" class=\"data row6 col16\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row6_col17\" class=\"data row6 col17\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row6_col18\" class=\"data row6 col18\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row6_col19\" class=\"data row6 col19\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row6_col20\" class=\"data row6 col20\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row6_col21\" class=\"data row6 col21\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row6_col22\" class=\"data row6 col22\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row6_col23\" class=\"data row6 col23\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row6_col24\" class=\"data row6 col24\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_51c51_level0_row7\" class=\"row_heading level0 row7\" >PEP11900D031</th>\n",
       "                        <td id=\"T_51c51_row7_col0\" class=\"data row7 col0\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row7_col1\" class=\"data row7 col1\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row7_col2\" class=\"data row7 col2\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row7_col3\" class=\"data row7 col3\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row7_col4\" class=\"data row7 col4\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row7_col5\" class=\"data row7 col5\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row7_col6\" class=\"data row7 col6\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row7_col7\" class=\"data row7 col7\" >-0.0012</td>\n",
       "                        <td id=\"T_51c51_row7_col8\" class=\"data row7 col8\" >-0.0057</td>\n",
       "                        <td id=\"T_51c51_row7_col9\" class=\"data row7 col9\" >-0.0192</td>\n",
       "                        <td id=\"T_51c51_row7_col10\" class=\"data row7 col10\" >-0.0730</td>\n",
       "                        <td id=\"T_51c51_row7_col11\" class=\"data row7 col11\" >-0.3685</td>\n",
       "                        <td id=\"T_51c51_row7_col12\" class=\"data row7 col12\" >-3.0416</td>\n",
       "                        <td id=\"T_51c51_row7_col13\" class=\"data row7 col13\" >-0.0030</td>\n",
       "                        <td id=\"T_51c51_row7_col14\" class=\"data row7 col14\" >-0.0000</td>\n",
       "                        <td id=\"T_51c51_row7_col15\" class=\"data row7 col15\" >-0.0000</td>\n",
       "                        <td id=\"T_51c51_row7_col16\" class=\"data row7 col16\" >0.0002</td>\n",
       "                        <td id=\"T_51c51_row7_col17\" class=\"data row7 col17\" >0.0016</td>\n",
       "                        <td id=\"T_51c51_row7_col18\" class=\"data row7 col18\" >0.0083</td>\n",
       "                        <td id=\"T_51c51_row7_col19\" class=\"data row7 col19\" >0.0464</td>\n",
       "                        <td id=\"T_51c51_row7_col20\" class=\"data row7 col20\" >0.3871</td>\n",
       "                        <td id=\"T_51c51_row7_col21\" class=\"data row7 col21\" >7.7809</td>\n",
       "                        <td id=\"T_51c51_row7_col22\" class=\"data row7 col22\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row7_col23\" class=\"data row7 col23\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row7_col24\" class=\"data row7 col24\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_51c51_level0_row8\" class=\"row_heading level0 row8\" >PEP13000D012</th>\n",
       "                        <td id=\"T_51c51_row8_col0\" class=\"data row8 col0\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row8_col1\" class=\"data row8 col1\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row8_col2\" class=\"data row8 col2\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row8_col3\" class=\"data row8 col3\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row8_col4\" class=\"data row8 col4\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row8_col5\" class=\"data row8 col5\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row8_col6\" class=\"data row8 col6\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row8_col7\" class=\"data row8 col7\" >-0.0000</td>\n",
       "                        <td id=\"T_51c51_row8_col8\" class=\"data row8 col8\" >-0.0078</td>\n",
       "                        <td id=\"T_51c51_row8_col9\" class=\"data row8 col9\" >-0.0142</td>\n",
       "                        <td id=\"T_51c51_row8_col10\" class=\"data row8 col10\" >-0.0617</td>\n",
       "                        <td id=\"T_51c51_row8_col11\" class=\"data row8 col11\" >-0.3327</td>\n",
       "                        <td id=\"T_51c51_row8_col12\" class=\"data row8 col12\" >-1.0902</td>\n",
       "                        <td id=\"T_51c51_row8_col13\" class=\"data row8 col13\" >-4.8055</td>\n",
       "                        <td id=\"T_51c51_row8_col14\" class=\"data row8 col14\" >-0.2074</td>\n",
       "                        <td id=\"T_51c51_row8_col15\" class=\"data row8 col15\" >-0.0000</td>\n",
       "                        <td id=\"T_51c51_row8_col16\" class=\"data row8 col16\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row8_col17\" class=\"data row8 col17\" >0.0029</td>\n",
       "                        <td id=\"T_51c51_row8_col18\" class=\"data row8 col18\" >0.0035</td>\n",
       "                        <td id=\"T_51c51_row8_col19\" class=\"data row8 col19\" >0.0378</td>\n",
       "                        <td id=\"T_51c51_row8_col20\" class=\"data row8 col20\" >0.3605</td>\n",
       "                        <td id=\"T_51c51_row8_col21\" class=\"data row8 col21\" >2.2492</td>\n",
       "                        <td id=\"T_51c51_row8_col22\" class=\"data row8 col22\" >22.6080</td>\n",
       "                        <td id=\"T_51c51_row8_col23\" class=\"data row8 col23\" >0.0553</td>\n",
       "                        <td id=\"T_51c51_row8_col24\" class=\"data row8 col24\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_51c51_level0_row9\" class=\"row_heading level0 row9\" >PEP13000M088</th>\n",
       "                        <td id=\"T_51c51_row9_col0\" class=\"data row9 col0\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row9_col1\" class=\"data row9 col1\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row9_col2\" class=\"data row9 col2\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row9_col3\" class=\"data row9 col3\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row9_col4\" class=\"data row9 col4\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row9_col5\" class=\"data row9 col5\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row9_col6\" class=\"data row9 col6\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row9_col7\" class=\"data row9 col7\" >-0.0013</td>\n",
       "                        <td id=\"T_51c51_row9_col8\" class=\"data row9 col8\" >-0.0004</td>\n",
       "                        <td id=\"T_51c51_row9_col9\" class=\"data row9 col9\" >-0.0147</td>\n",
       "                        <td id=\"T_51c51_row9_col10\" class=\"data row9 col10\" >-0.0501</td>\n",
       "                        <td id=\"T_51c51_row9_col11\" class=\"data row9 col11\" >-0.2770</td>\n",
       "                        <td id=\"T_51c51_row9_col12\" class=\"data row9 col12\" >-2.4626</td>\n",
       "                        <td id=\"T_51c51_row9_col13\" class=\"data row9 col13\" >-3.0764</td>\n",
       "                        <td id=\"T_51c51_row9_col14\" class=\"data row9 col14\" >-0.0000</td>\n",
       "                        <td id=\"T_51c51_row9_col15\" class=\"data row9 col15\" >-0.0000</td>\n",
       "                        <td id=\"T_51c51_row9_col16\" class=\"data row9 col16\" >0.0005</td>\n",
       "                        <td id=\"T_51c51_row9_col17\" class=\"data row9 col17\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row9_col18\" class=\"data row9 col18\" >0.0096</td>\n",
       "                        <td id=\"T_51c51_row9_col19\" class=\"data row9 col19\" >0.0368</td>\n",
       "                        <td id=\"T_51c51_row9_col20\" class=\"data row9 col20\" >0.3081</td>\n",
       "                        <td id=\"T_51c51_row9_col21\" class=\"data row9 col21\" >4.4240</td>\n",
       "                        <td id=\"T_51c51_row9_col22\" class=\"data row9 col22\" >8.1440</td>\n",
       "                        <td id=\"T_51c51_row9_col23\" class=\"data row9 col23\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row9_col24\" class=\"data row9 col24\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_51c51_level0_row10\" class=\"row_heading level0 row10\" >PEP23900M103</th>\n",
       "                        <td id=\"T_51c51_row10_col0\" class=\"data row10 col0\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row10_col1\" class=\"data row10 col1\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row10_col2\" class=\"data row10 col2\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row10_col3\" class=\"data row10 col3\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row10_col4\" class=\"data row10 col4\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row10_col5\" class=\"data row10 col5\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row10_col6\" class=\"data row10 col6\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row10_col7\" class=\"data row10 col7\" >-0.0000</td>\n",
       "                        <td id=\"T_51c51_row10_col8\" class=\"data row10 col8\" >-0.0005</td>\n",
       "                        <td id=\"T_51c51_row10_col9\" class=\"data row10 col9\" >-0.0117</td>\n",
       "                        <td id=\"T_51c51_row10_col10\" class=\"data row10 col10\" >-0.0405</td>\n",
       "                        <td id=\"T_51c51_row10_col11\" class=\"data row10 col11\" >-0.2274</td>\n",
       "                        <td id=\"T_51c51_row10_col12\" class=\"data row10 col12\" >-3.9726</td>\n",
       "                        <td id=\"T_51c51_row10_col13\" class=\"data row10 col13\" >-0.0381</td>\n",
       "                        <td id=\"T_51c51_row10_col14\" class=\"data row10 col14\" >-0.0000</td>\n",
       "                        <td id=\"T_51c51_row10_col15\" class=\"data row10 col15\" >-0.0000</td>\n",
       "                        <td id=\"T_51c51_row10_col16\" class=\"data row10 col16\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row10_col17\" class=\"data row10 col17\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row10_col18\" class=\"data row10 col18\" >0.0078</td>\n",
       "                        <td id=\"T_51c51_row10_col19\" class=\"data row10 col19\" >0.0322</td>\n",
       "                        <td id=\"T_51c51_row10_col20\" class=\"data row10 col20\" >0.2581</td>\n",
       "                        <td id=\"T_51c51_row10_col21\" class=\"data row10 col21\" >11.0636</td>\n",
       "                        <td id=\"T_51c51_row10_col22\" class=\"data row10 col22\" >0.0011</td>\n",
       "                        <td id=\"T_51c51_row10_col23\" class=\"data row10 col23\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row10_col24\" class=\"data row10 col24\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_51c51_level0_row11\" class=\"row_heading level0 row11\" >PEP70101M530</th>\n",
       "                        <td id=\"T_51c51_row11_col0\" class=\"data row11 col0\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row11_col1\" class=\"data row11 col1\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row11_col2\" class=\"data row11 col2\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row11_col3\" class=\"data row11 col3\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row11_col4\" class=\"data row11 col4\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row11_col5\" class=\"data row11 col5\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row11_col6\" class=\"data row11 col6\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row11_col7\" class=\"data row11 col7\" >-0.0000</td>\n",
       "                        <td id=\"T_51c51_row11_col8\" class=\"data row11 col8\" >-0.0052</td>\n",
       "                        <td id=\"T_51c51_row11_col9\" class=\"data row11 col9\" >-0.0101</td>\n",
       "                        <td id=\"T_51c51_row11_col10\" class=\"data row11 col10\" >-0.0442</td>\n",
       "                        <td id=\"T_51c51_row11_col11\" class=\"data row11 col11\" >-0.2488</td>\n",
       "                        <td id=\"T_51c51_row11_col12\" class=\"data row11 col12\" >-0.8826</td>\n",
       "                        <td id=\"T_51c51_row11_col13\" class=\"data row11 col13\" >-4.9147</td>\n",
       "                        <td id=\"T_51c51_row11_col14\" class=\"data row11 col14\" >-3.5537</td>\n",
       "                        <td id=\"T_51c51_row11_col15\" class=\"data row11 col15\" >-0.0000</td>\n",
       "                        <td id=\"T_51c51_row11_col16\" class=\"data row11 col16\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row11_col17\" class=\"data row11 col17\" >0.0019</td>\n",
       "                        <td id=\"T_51c51_row11_col18\" class=\"data row11 col18\" >0.0026</td>\n",
       "                        <td id=\"T_51c51_row11_col19\" class=\"data row11 col19\" >0.0280</td>\n",
       "                        <td id=\"T_51c51_row11_col20\" class=\"data row11 col20\" >0.2765</td>\n",
       "                        <td id=\"T_51c51_row11_col21\" class=\"data row11 col21\" >1.8686</td>\n",
       "                        <td id=\"T_51c51_row11_col22\" class=\"data row11 col22\" >19.1157</td>\n",
       "                        <td id=\"T_51c51_row11_col23\" class=\"data row11 col23\" >13.0732</td>\n",
       "                        <td id=\"T_51c51_row11_col24\" class=\"data row11 col24\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_51c51_level0_row12\" class=\"row_heading level0 row12\" >PEP70101M571</th>\n",
       "                        <td id=\"T_51c51_row12_col0\" class=\"data row12 col0\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row12_col1\" class=\"data row12 col1\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row12_col2\" class=\"data row12 col2\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row12_col3\" class=\"data row12 col3\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row12_col4\" class=\"data row12 col4\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row12_col5\" class=\"data row12 col5\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row12_col6\" class=\"data row12 col6\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row12_col7\" class=\"data row12 col7\" >-0.0015</td>\n",
       "                        <td id=\"T_51c51_row12_col8\" class=\"data row12 col8\" >-0.0039</td>\n",
       "                        <td id=\"T_51c51_row12_col9\" class=\"data row12 col9\" >-0.0126</td>\n",
       "                        <td id=\"T_51c51_row12_col10\" class=\"data row12 col10\" >-0.0501</td>\n",
       "                        <td id=\"T_51c51_row12_col11\" class=\"data row12 col11\" >-0.2829</td>\n",
       "                        <td id=\"T_51c51_row12_col12\" class=\"data row12 col12\" >-1.0108</td>\n",
       "                        <td id=\"T_51c51_row12_col13\" class=\"data row12 col13\" >-2.5878</td>\n",
       "                        <td id=\"T_51c51_row12_col14\" class=\"data row12 col14\" >-6.0312</td>\n",
       "                        <td id=\"T_51c51_row12_col15\" class=\"data row12 col15\" >-0.4501</td>\n",
       "                        <td id=\"T_51c51_row12_col16\" class=\"data row12 col16\" >0.0002</td>\n",
       "                        <td id=\"T_51c51_row12_col17\" class=\"data row12 col17\" >0.0016</td>\n",
       "                        <td id=\"T_51c51_row12_col18\" class=\"data row12 col18\" >0.0064</td>\n",
       "                        <td id=\"T_51c51_row12_col19\" class=\"data row12 col19\" >0.0319</td>\n",
       "                        <td id=\"T_51c51_row12_col20\" class=\"data row12 col20\" >0.3123</td>\n",
       "                        <td id=\"T_51c51_row12_col21\" class=\"data row12 col21\" >2.1336</td>\n",
       "                        <td id=\"T_51c51_row12_col22\" class=\"data row12 col22\" >10.1632</td>\n",
       "                        <td id=\"T_51c51_row12_col23\" class=\"data row12 col23\" >49.9021</td>\n",
       "                        <td id=\"T_51c51_row12_col24\" class=\"data row12 col24\" >0.4523</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_51c51_level0_row13\" class=\"row_heading level0 row13\" >PEP70310M156</th>\n",
       "                        <td id=\"T_51c51_row13_col0\" class=\"data row13 col0\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row13_col1\" class=\"data row13 col1\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row13_col2\" class=\"data row13 col2\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row13_col3\" class=\"data row13 col3\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row13_col4\" class=\"data row13 col4\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row13_col5\" class=\"data row13 col5\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row13_col6\" class=\"data row13 col6\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row13_col7\" class=\"data row13 col7\" >-0.0000</td>\n",
       "                        <td id=\"T_51c51_row13_col8\" class=\"data row13 col8\" >-0.0039</td>\n",
       "                        <td id=\"T_51c51_row13_col9\" class=\"data row13 col9\" >-0.0097</td>\n",
       "                        <td id=\"T_51c51_row13_col10\" class=\"data row13 col10\" >-0.0403</td>\n",
       "                        <td id=\"T_51c51_row13_col11\" class=\"data row13 col11\" >-0.2614</td>\n",
       "                        <td id=\"T_51c51_row13_col12\" class=\"data row13 col12\" >-3.8920</td>\n",
       "                        <td id=\"T_51c51_row13_col13\" class=\"data row13 col13\" >-0.0000</td>\n",
       "                        <td id=\"T_51c51_row13_col14\" class=\"data row13 col14\" >-0.0000</td>\n",
       "                        <td id=\"T_51c51_row13_col15\" class=\"data row13 col15\" >-0.0000</td>\n",
       "                        <td id=\"T_51c51_row13_col16\" class=\"data row13 col16\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row13_col17\" class=\"data row13 col17\" >0.0010</td>\n",
       "                        <td id=\"T_51c51_row13_col18\" class=\"data row13 col18\" >0.0030</td>\n",
       "                        <td id=\"T_51c51_row13_col19\" class=\"data row13 col19\" >0.0268</td>\n",
       "                        <td id=\"T_51c51_row13_col20\" class=\"data row13 col20\" >0.2508</td>\n",
       "                        <td id=\"T_51c51_row13_col21\" class=\"data row13 col21\" >10.6813</td>\n",
       "                        <td id=\"T_51c51_row13_col22\" class=\"data row13 col22\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row13_col23\" class=\"data row13 col23\" >0.0000</td>\n",
       "                        <td id=\"T_51c51_row13_col24\" class=\"data row13 col24\" >0.0000</td>\n",
       "            </tr>\n",
       "    </tbody></table>"
      ],
      "text/plain": [
       "<pandas.io.formats.style.Styler at 0x7ff2806f7e20>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "########################################################################\n",
    "# Building The Loadings Matrix\n",
    "########################################################################\n",
    "\n",
    "a = np.identity(len(cols))\n",
    "a = pd.DataFrame(a, index=cols, columns=cols)\n",
    "loadings = pd.concat([a, loadings], axis = 1)\n",
    "loadings.fillna(0, inplace=True)\n",
    "\n",
    "display(loadings.style.format(\"{:.4f}\").background_gradient(cmap='YlGn'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.2 Calculating the portfolio that maximizes Sharpe ratio."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style  type=\"text/css\" >\n",
       "#T_80973_row0_col0,#T_80973_row5_col0,#T_80973_row7_col0,#T_80973_row8_col0,#T_80973_row9_col0,#T_80973_row10_col0,#T_80973_row13_col0{\n",
       "            background-color:  #ffffe5;\n",
       "            color:  #000000;\n",
       "        }#T_80973_row1_col0{\n",
       "            background-color:  #74c477;\n",
       "            color:  #000000;\n",
       "        }#T_80973_row2_col0{\n",
       "            background-color:  #a1d889;\n",
       "            color:  #000000;\n",
       "        }#T_80973_row3_col0{\n",
       "            background-color:  #56b568;\n",
       "            color:  #000000;\n",
       "        }#T_80973_row4_col0{\n",
       "            background-color:  #004529;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_80973_row6_col0{\n",
       "            background-color:  #c3e698;\n",
       "            color:  #000000;\n",
       "        }#T_80973_row11_col0{\n",
       "            background-color:  #c5e89a;\n",
       "            color:  #000000;\n",
       "        }#T_80973_row12_col0{\n",
       "            background-color:  #2b8e4a;\n",
       "            color:  #000000;\n",
       "        }</style><table id=\"T_80973_\" ><thead>    <tr>        <th class=\"blank level0\" ></th>        <th class=\"col_heading level0 col0\" >weights</th>    </tr></thead><tbody>\n",
       "                <tr>\n",
       "                        <th id=\"T_80973_level0_row0\" class=\"row_heading level0 row0\" >APA</th>\n",
       "                        <td id=\"T_80973_row0_col0\" class=\"data row0 col0\" >0.0000%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_80973_level0_row1\" class=\"row_heading level0 row1\" >CMCSA</th>\n",
       "                        <td id=\"T_80973_row1_col0\" class=\"data row1 col0\" >13.3020%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_80973_level0_row2\" class=\"row_heading level0 row2\" >CNP</th>\n",
       "                        <td id=\"T_80973_row2_col0\" class=\"data row2 col0\" >10.5311%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_80973_level0_row3\" class=\"row_heading level0 row3\" >HPQ</th>\n",
       "                        <td id=\"T_80973_row3_col0\" class=\"data row3 col0\" >15.1024%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_80973_level0_row4\" class=\"row_heading level0 row4\" >PSA</th>\n",
       "                        <td id=\"T_80973_row4_col0\" class=\"data row4 col0\" >26.1345%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_80973_level0_row5\" class=\"row_heading level0 row5\" >SEE</th>\n",
       "                        <td id=\"T_80973_row5_col0\" class=\"data row5 col0\" >0.0000%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_80973_level0_row6\" class=\"row_heading level0 row6\" >ZION</th>\n",
       "                        <td id=\"T_80973_row6_col0\" class=\"data row6 col0\" >8.2247%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_80973_level0_row7\" class=\"row_heading level0 row7\" >PEP11900D031</th>\n",
       "                        <td id=\"T_80973_row7_col0\" class=\"data row7 col0\" >0.0000%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_80973_level0_row8\" class=\"row_heading level0 row8\" >PEP13000D012</th>\n",
       "                        <td id=\"T_80973_row8_col0\" class=\"data row8 col0\" >0.0002%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_80973_level0_row9\" class=\"row_heading level0 row9\" >PEP13000M088</th>\n",
       "                        <td id=\"T_80973_row9_col0\" class=\"data row9 col0\" >0.0000%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_80973_level0_row10\" class=\"row_heading level0 row10\" >PEP23900M103</th>\n",
       "                        <td id=\"T_80973_row10_col0\" class=\"data row10 col0\" >0.0000%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_80973_level0_row11\" class=\"row_heading level0 row11\" >PEP70101M530</th>\n",
       "                        <td id=\"T_80973_row11_col0\" class=\"data row11 col0\" >7.9932%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_80973_level0_row12\" class=\"row_heading level0 row12\" >PEP70101M571</th>\n",
       "                        <td id=\"T_80973_row12_col0\" class=\"data row12 col0\" >18.7118%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_80973_level0_row13\" class=\"row_heading level0 row13\" >PEP70310M156</th>\n",
       "                        <td id=\"T_80973_row13_col0\" class=\"data row13 col0\" >0.0000%</td>\n",
       "            </tr>\n",
       "    </tbody></table>"
      ],
      "text/plain": [
       "<pandas.io.formats.style.Styler at 0x7ff285015880>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "########################################################################\n",
    "# Calculating optimum portfolio\n",
    "########################################################################\n",
    "\n",
    "port = pf.Portfolio(returns=Y)\n",
    "\n",
    "# Select method and estimate input parameters:\n",
    "\n",
    "method_mu='hist' # Method to estimate expected returns based on historical data.\n",
    "method_cov='hist' # Method to estimate covariance matrix based on historical data.\n",
    "\n",
    "port.assets_stats(method_mu=method_mu, method_cov=method_cov, d=0.94)\n",
    "\n",
    "port.factors = X\n",
    "port.factors_stats(method_mu=method_mu, method_cov=method_cov, d=0.94, B=loadings)\n",
    "\n",
    "# Estimate optimal portfolio:\n",
    "\n",
    "model='FM' # Factor Model\n",
    "rm = 'MV' # Risk measure used, this time will be variance\n",
    "obj = 'Sharpe' # Objective function, could be MinRisk, MaxRet, Utility or Sharpe\n",
    "hist = False # Use historical scenarios for risk measures that depend on scenarios\n",
    "rf = 0 # Risk free rate\n",
    "l = 0 # Risk aversion factor, only useful when obj is 'Utility'\n",
    "\n",
    "w = port.optimization(model=model, rm=rm, obj=obj, rf=rf, l=l, hist=hist)\n",
    "\n",
    "display(w.style.format(\"{:.4%}\").background_gradient(cmap='YlGn'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5. Optimization of Equity and Bond Portfolio with Key Rate Durations Constraints\n",
    "\n",
    "This part shows how __Riskfolio-Lib__ can be used to build immunized portfolios using __duration matching__ and __convexity matching__, however the example only use duration matching. More information about inmunization theory can be found in this __[link](https://www.investopedia.com/terms/i/immunization.asp)__.\n",
    "\n",
    "### 5.1 Statistics of Risk Factors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style  type=\"text/css\" >\n",
       "#T_48e4d_row0_col0,#T_48e4d_row0_col1,#T_48e4d_row1_col0,#T_48e4d_row1_col1,#T_48e4d_row2_col0,#T_48e4d_row2_col1,#T_48e4d_row3_col0,#T_48e4d_row3_col1,#T_48e4d_row4_col0,#T_48e4d_row4_col1,#T_48e4d_row5_col0,#T_48e4d_row5_col1,#T_48e4d_row6_col0,#T_48e4d_row6_col1,#T_48e4d_row7_col0,#T_48e4d_row8_col0,#T_48e4d_row9_col0{\n",
       "            background-color:  #004529;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_48e4d_row7_col1,#T_48e4d_row8_col1,#T_48e4d_row9_col1,#T_48e4d_row10_col1,#T_48e4d_row11_col1,#T_48e4d_row12_col1,#T_48e4d_row13_col1,#T_48e4d_row14_col0,#T_48e4d_row14_col1,#T_48e4d_row15_col1{\n",
       "            background-color:  #ffffe5;\n",
       "            color:  #000000;\n",
       "        }#T_48e4d_row10_col0{\n",
       "            background-color:  #00482a;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_48e4d_row11_col0{\n",
       "            background-color:  #005530;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_48e4d_row12_col0{\n",
       "            background-color:  #b9e294;\n",
       "            color:  #000000;\n",
       "        }#T_48e4d_row13_col0{\n",
       "            background-color:  #e9f6af;\n",
       "            color:  #000000;\n",
       "        }#T_48e4d_row15_col0{\n",
       "            background-color:  #005a31;\n",
       "            color:  #f1f1f1;\n",
       "        }</style><table id=\"T_48e4d_\" ><thead>    <tr>        <th class=\"blank level0\" ></th>        <th class=\"col_heading level0 col0\" >min</th>        <th class=\"col_heading level0 col1\" >max</th>    </tr></thead><tbody>\n",
       "                <tr>\n",
       "                        <th id=\"T_48e4d_level0_row0\" class=\"row_heading level0 row0\" >APA</th>\n",
       "                        <td id=\"T_48e4d_row0_col0\" class=\"data row0 col0\" >0.0000</td>\n",
       "                        <td id=\"T_48e4d_row0_col1\" class=\"data row0 col1\" >1.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_48e4d_level0_row1\" class=\"row_heading level0 row1\" >CMCSA</th>\n",
       "                        <td id=\"T_48e4d_row1_col0\" class=\"data row1 col0\" >0.0000</td>\n",
       "                        <td id=\"T_48e4d_row1_col1\" class=\"data row1 col1\" >1.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_48e4d_level0_row2\" class=\"row_heading level0 row2\" >CNP</th>\n",
       "                        <td id=\"T_48e4d_row2_col0\" class=\"data row2 col0\" >0.0000</td>\n",
       "                        <td id=\"T_48e4d_row2_col1\" class=\"data row2 col1\" >1.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_48e4d_level0_row3\" class=\"row_heading level0 row3\" >HPQ</th>\n",
       "                        <td id=\"T_48e4d_row3_col0\" class=\"data row3 col0\" >0.0000</td>\n",
       "                        <td id=\"T_48e4d_row3_col1\" class=\"data row3 col1\" >1.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_48e4d_level0_row4\" class=\"row_heading level0 row4\" >PSA</th>\n",
       "                        <td id=\"T_48e4d_row4_col0\" class=\"data row4 col0\" >0.0000</td>\n",
       "                        <td id=\"T_48e4d_row4_col1\" class=\"data row4 col1\" >1.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_48e4d_level0_row5\" class=\"row_heading level0 row5\" >SEE</th>\n",
       "                        <td id=\"T_48e4d_row5_col0\" class=\"data row5 col0\" >0.0000</td>\n",
       "                        <td id=\"T_48e4d_row5_col1\" class=\"data row5 col1\" >1.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_48e4d_level0_row6\" class=\"row_heading level0 row6\" >ZION</th>\n",
       "                        <td id=\"T_48e4d_row6_col0\" class=\"data row6 col0\" >0.0000</td>\n",
       "                        <td id=\"T_48e4d_row6_col1\" class=\"data row6 col1\" >1.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_48e4d_level0_row7\" class=\"row_heading level0 row7\" >R 0</th>\n",
       "                        <td id=\"T_48e4d_row7_col0\" class=\"data row7 col0\" >-0.0015</td>\n",
       "                        <td id=\"T_48e4d_row7_col1\" class=\"data row7 col1\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_48e4d_level0_row8\" class=\"row_heading level0 row8\" >R 90</th>\n",
       "                        <td id=\"T_48e4d_row8_col0\" class=\"data row8 col0\" >-0.0078</td>\n",
       "                        <td id=\"T_48e4d_row8_col1\" class=\"data row8 col1\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_48e4d_level0_row9\" class=\"row_heading level0 row9\" >R 180</th>\n",
       "                        <td id=\"T_48e4d_row9_col0\" class=\"data row9 col0\" >-0.0192</td>\n",
       "                        <td id=\"T_48e4d_row9_col1\" class=\"data row9 col1\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_48e4d_level0_row10\" class=\"row_heading level0 row10\" >R 360</th>\n",
       "                        <td id=\"T_48e4d_row10_col0\" class=\"data row10 col0\" >-0.0730</td>\n",
       "                        <td id=\"T_48e4d_row10_col1\" class=\"data row10 col1\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_48e4d_level0_row11\" class=\"row_heading level0 row11\" >R 720</th>\n",
       "                        <td id=\"T_48e4d_row11_col0\" class=\"data row11 col0\" >-0.3685</td>\n",
       "                        <td id=\"T_48e4d_row11_col1\" class=\"data row11 col1\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_48e4d_level0_row12\" class=\"row_heading level0 row12\" >R 1800</th>\n",
       "                        <td id=\"T_48e4d_row12_col0\" class=\"data row12 col0\" >-3.9726</td>\n",
       "                        <td id=\"T_48e4d_row12_col1\" class=\"data row12 col1\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_48e4d_level0_row13\" class=\"row_heading level0 row13\" >R 3600</th>\n",
       "                        <td id=\"T_48e4d_row13_col0\" class=\"data row13 col0\" >-4.9147</td>\n",
       "                        <td id=\"T_48e4d_row13_col1\" class=\"data row13 col1\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_48e4d_level0_row14\" class=\"row_heading level0 row14\" >R 7200</th>\n",
       "                        <td id=\"T_48e4d_row14_col0\" class=\"data row14 col0\" >-6.0312</td>\n",
       "                        <td id=\"T_48e4d_row14_col1\" class=\"data row14 col1\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_48e4d_level0_row15\" class=\"row_heading level0 row15\" >R 10800</th>\n",
       "                        <td id=\"T_48e4d_row15_col0\" class=\"data row15 col0\" >-0.4501</td>\n",
       "                        <td id=\"T_48e4d_row15_col1\" class=\"data row15 col1\" >0.0000</td>\n",
       "            </tr>\n",
       "    </tbody></table>"
      ],
      "text/plain": [
       "<pandas.io.formats.style.Styler at 0x7ff2807d5460>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<style  type=\"text/css\" >\n",
       "#T_3acc1_row0_col0,#T_3acc1_row1_col1,#T_3acc1_row2_col2,#T_3acc1_row3_col3,#T_3acc1_row4_col4,#T_3acc1_row5_col5,#T_3acc1_row6_col6,#T_3acc1_row7_col7,#T_3acc1_row8_col8,#T_3acc1_row9_col9,#T_3acc1_row10_col10,#T_3acc1_row11_col11,#T_3acc1_row12_col12,#T_3acc1_row13_col13,#T_3acc1_row14_col14,#T_3acc1_row15_col15{\n",
       "            background-color:  #004529;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_3acc1_row0_col1,#T_3acc1_row1_col3,#T_3acc1_row2_col3,#T_3acc1_row12_col10{\n",
       "            background-color:  #c3e698;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row0_col2,#T_3acc1_row5_col3,#T_3acc1_row9_col11{\n",
       "            background-color:  #a9db8c;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row0_col3{\n",
       "            background-color:  #afde8f;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row0_col4,#T_3acc1_row8_col11{\n",
       "            background-color:  #f2fab5;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row0_col5,#T_3acc1_row4_col3{\n",
       "            background-color:  #dbf1a4;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row0_col6,#T_3acc1_row6_col3{\n",
       "            background-color:  #9ad587;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row0_col7,#T_3acc1_row0_col10,#T_3acc1_row1_col15,#T_3acc1_row4_col7,#T_3acc1_row4_col12,#T_3acc1_row7_col1,#T_3acc1_row7_col3,#T_3acc1_row10_col3{\n",
       "            background-color:  #fcfed7;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row0_col8,#T_3acc1_row0_col9,#T_3acc1_row1_col7,#T_3acc1_row2_col7,#T_3acc1_row2_col9,#T_3acc1_row6_col10,#T_3acc1_row8_col3,#T_3acc1_row9_col3{\n",
       "            background-color:  #fdfed9;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row0_col11,#T_3acc1_row2_col14,#T_3acc1_row5_col15,#T_3acc1_row6_col8,#T_3acc1_row8_col5,#T_3acc1_row9_col1,#T_3acc1_row12_col1{\n",
       "            background-color:  #feffe1;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row0_col12,#T_3acc1_row1_col8,#T_3acc1_row3_col10,#T_3acc1_row6_col9,#T_3acc1_row11_col6{\n",
       "            background-color:  #feffdf;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row0_col13,#T_3acc1_row1_col14,#T_3acc1_row2_col8,#T_3acc1_row11_col3,#T_3acc1_row15_col1,#T_3acc1_row15_col2{\n",
       "            background-color:  #fdfeda;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row0_col14,#T_3acc1_row4_col9,#T_3acc1_row6_col11,#T_3acc1_row14_col0,#T_3acc1_row15_col6{\n",
       "            background-color:  #fcfed4;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row0_col15,#T_3acc1_row1_col12,#T_3acc1_row4_col8,#T_3acc1_row4_col11,#T_3acc1_row13_col6{\n",
       "            background-color:  #fcfed6;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row1_col0{\n",
       "            background-color:  #c0e597;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row1_col2{\n",
       "            background-color:  #b5e092;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row1_col4{\n",
       "            background-color:  #d3eda0;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row1_col5{\n",
       "            background-color:  #b9e294;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row1_col6,#T_3acc1_row5_col6,#T_3acc1_row11_col9{\n",
       "            background-color:  #acdd8e;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row1_col9,#T_3acc1_row1_col10,#T_3acc1_row2_col11,#T_3acc1_row2_col12,#T_3acc1_row2_col13,#T_3acc1_row3_col8,#T_3acc1_row3_col9,#T_3acc1_row3_col12,#T_3acc1_row3_col13,#T_3acc1_row4_col14,#T_3acc1_row4_col15,#T_3acc1_row5_col9,#T_3acc1_row8_col6,#T_3acc1_row9_col6,#T_3acc1_row10_col1,#T_3acc1_row11_col7,#T_3acc1_row12_col0,#T_3acc1_row12_col2,#T_3acc1_row12_col3,#T_3acc1_row13_col3,#T_3acc1_row13_col4,#T_3acc1_row14_col4,#T_3acc1_row15_col5{\n",
       "            background-color:  #ffffe5;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row1_col11,#T_3acc1_row2_col15,#T_3acc1_row3_col7,#T_3acc1_row3_col11,#T_3acc1_row4_col13,#T_3acc1_row5_col8,#T_3acc1_row7_col6,#T_3acc1_row10_col6,#T_3acc1_row13_col2{\n",
       "            background-color:  #feffe2;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row1_col13,#T_3acc1_row2_col10,#T_3acc1_row5_col7,#T_3acc1_row6_col7,#T_3acc1_row7_col5,#T_3acc1_row8_col1,#T_3acc1_row12_col6,#T_3acc1_row13_col5,#T_3acc1_row14_col2{\n",
       "            background-color:  #fdfedb;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row2_col0{\n",
       "            background-color:  #aedd8e;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row2_col1,#T_3acc1_row10_col12{\n",
       "            background-color:  #bce395;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row2_col4,#T_3acc1_row3_col6{\n",
       "            background-color:  #a7db8c;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row2_col5,#T_3acc1_row3_col0{\n",
       "            background-color:  #b2df90;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row2_col6,#T_3acc1_row14_col11{\n",
       "            background-color:  #e1f3a9;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row3_col1,#T_3acc1_row4_col5{\n",
       "            background-color:  #c9e99c;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row3_col2{\n",
       "            background-color:  #c1e698;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row3_col4{\n",
       "            background-color:  #ddf2a6;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row3_col5{\n",
       "            background-color:  #b1df90;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row3_col14,#T_3acc1_row3_col15,#T_3acc1_row7_col11,#T_3acc1_row9_col5,#T_3acc1_row11_col1,#T_3acc1_row13_col1,#T_3acc1_row14_col5,#T_3acc1_row15_col4{\n",
       "            background-color:  #ffffe4;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row4_col0,#T_3acc1_row10_col13{\n",
       "            background-color:  #f3fab6;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row4_col1,#T_3acc1_row5_col0,#T_3acc1_row6_col2{\n",
       "            background-color:  #d6efa2;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row4_col2{\n",
       "            background-color:  #a2d88a;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row4_col6,#T_3acc1_row6_col13,#T_3acc1_row15_col8{\n",
       "            background-color:  #f9fdc5;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row4_col10,#T_3acc1_row10_col0,#T_3acc1_row11_col4,#T_3acc1_row11_col5,#T_3acc1_row14_col6,#T_3acc1_row15_col0{\n",
       "            background-color:  #fcfed3;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row5_col1{\n",
       "            background-color:  #b8e293;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row5_col2{\n",
       "            background-color:  #abdc8d;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row5_col4{\n",
       "            background-color:  #c4e799;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row5_col10,#T_3acc1_row11_col2,#T_3acc1_row12_col4,#T_3acc1_row15_col3{\n",
       "            background-color:  #fdfedd;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row5_col11,#T_3acc1_row9_col0,#T_3acc1_row10_col2,#T_3acc1_row12_col5{\n",
       "            background-color:  #fbfed0;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row5_col12,#T_3acc1_row7_col13,#T_3acc1_row13_col9,#T_3acc1_row15_col7{\n",
       "            background-color:  #f9fdc4;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row5_col13,#T_3acc1_row7_col0,#T_3acc1_row7_col4,#T_3acc1_row8_col0{\n",
       "            background-color:  #fbfed2;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row5_col14,#T_3acc1_row10_col5,#T_3acc1_row11_col0,#T_3acc1_row13_col0,#T_3acc1_row14_col1,#T_3acc1_row14_col3{\n",
       "            background-color:  #feffde;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row6_col0{\n",
       "            background-color:  #90d083;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row6_col1,#T_3acc1_row6_col5{\n",
       "            background-color:  #a6da8b;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row6_col4,#T_3acc1_row9_col13,#T_3acc1_row9_col14,#T_3acc1_row9_col15{\n",
       "            background-color:  #f7fcb9;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row6_col12,#T_3acc1_row12_col8,#T_3acc1_row13_col8,#T_3acc1_row14_col8{\n",
       "            background-color:  #fafdc8;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row6_col14,#T_3acc1_row7_col14{\n",
       "            background-color:  #f9fdc7;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row6_col15,#T_3acc1_row9_col2,#T_3acc1_row12_col7,#T_3acc1_row14_col7{\n",
       "            background-color:  #fafdcb;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row7_col2,#T_3acc1_row8_col4{\n",
       "            background-color:  #fbfdce;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row7_col8,#T_3acc1_row8_col7{\n",
       "            background-color:  #005931;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_3acc1_row7_col9{\n",
       "            background-color:  #288a47;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row7_col10{\n",
       "            background-color:  #cfec9e;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row7_col12,#T_3acc1_row8_col15,#T_3acc1_row14_col9{\n",
       "            background-color:  #f8fcc0;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row7_col15,#T_3acc1_row8_col14{\n",
       "            background-color:  #f8fdc1;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row8_col2,#T_3acc1_row9_col4,#T_3acc1_row13_col7{\n",
       "            background-color:  #fafdcc;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row8_col9,#T_3acc1_row9_col8,#T_3acc1_row14_col15,#T_3acc1_row15_col14{\n",
       "            background-color:  #005a31;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_3acc1_row8_col10{\n",
       "            background-color:  #68be71;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row8_col12{\n",
       "            background-color:  #f7fcbc;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row8_col13{\n",
       "            background-color:  #f8fcbd;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row9_col7{\n",
       "            background-color:  #278946;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row9_col10,#T_3acc1_row10_col9,#T_3acc1_row10_col11,#T_3acc1_row11_col10{\n",
       "            background-color:  #15793e;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_3acc1_row9_col12,#T_3acc1_row15_col10{\n",
       "            background-color:  #edf8b1;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row10_col4{\n",
       "            background-color:  #fbfdcf;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row10_col7{\n",
       "            background-color:  #d0ec9f;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row10_col8{\n",
       "            background-color:  #69bf72;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row10_col14{\n",
       "            background-color:  #edf8b2;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row10_col15{\n",
       "            background-color:  #ecf7b1;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row11_col8,#T_3acc1_row12_col9{\n",
       "            background-color:  #f5fbb8;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row11_col12{\n",
       "            background-color:  #3fa85b;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row11_col13,#T_3acc1_row11_col15,#T_3acc1_row15_col11{\n",
       "            background-color:  #dcf1a5;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row11_col14,#T_3acc1_row13_col11{\n",
       "            background-color:  #dff3a8;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row12_col11{\n",
       "            background-color:  #42ab5d;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row12_col13,#T_3acc1_row13_col12{\n",
       "            background-color:  #218242;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row12_col14{\n",
       "            background-color:  #7ac77a;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row12_col15{\n",
       "            background-color:  #a1d889;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row13_col10{\n",
       "            background-color:  #f7fcba;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row13_col14,#T_3acc1_row14_col13{\n",
       "            background-color:  #0e743c;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_3acc1_row13_col15{\n",
       "            background-color:  #3fa95c;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row14_col10{\n",
       "            background-color:  #eff9b3;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row14_col12{\n",
       "            background-color:  #77c679;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row15_col9{\n",
       "            background-color:  #f8fcbe;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row15_col12{\n",
       "            background-color:  #9cd687;\n",
       "            color:  #000000;\n",
       "        }#T_3acc1_row15_col13{\n",
       "            background-color:  #3ea75a;\n",
       "            color:  #000000;\n",
       "        }</style><table id=\"T_3acc1_\" ><thead>    <tr>        <th class=\"blank level0\" ></th>        <th class=\"col_heading level0 col0\" >APA</th>        <th class=\"col_heading level0 col1\" >CMCSA</th>        <th class=\"col_heading level0 col2\" >CNP</th>        <th class=\"col_heading level0 col3\" >HPQ</th>        <th class=\"col_heading level0 col4\" >PSA</th>        <th class=\"col_heading level0 col5\" >SEE</th>        <th class=\"col_heading level0 col6\" >ZION</th>        <th class=\"col_heading level0 col7\" >R 0</th>        <th class=\"col_heading level0 col8\" >R 90</th>        <th class=\"col_heading level0 col9\" >R 180</th>        <th class=\"col_heading level0 col10\" >R 360</th>        <th class=\"col_heading level0 col11\" >R 720</th>        <th class=\"col_heading level0 col12\" >R 1800</th>        <th class=\"col_heading level0 col13\" >R 3600</th>        <th class=\"col_heading level0 col14\" >R 7200</th>        <th class=\"col_heading level0 col15\" >R 10800</th>    </tr></thead><tbody>\n",
       "                <tr>\n",
       "                        <th id=\"T_3acc1_level0_row0\" class=\"row_heading level0 row0\" >APA</th>\n",
       "                        <td id=\"T_3acc1_row0_col0\" class=\"data row0 col0\" >1.0000</td>\n",
       "                        <td id=\"T_3acc1_row0_col1\" class=\"data row0 col1\" >0.2775</td>\n",
       "                        <td id=\"T_3acc1_row0_col2\" class=\"data row0 col2\" >0.3307</td>\n",
       "                        <td id=\"T_3acc1_row0_col3\" class=\"data row0 col3\" >0.3190</td>\n",
       "                        <td id=\"T_3acc1_row0_col4\" class=\"data row0 col4\" >0.0872</td>\n",
       "                        <td id=\"T_3acc1_row0_col5\" class=\"data row0 col5\" >0.2098</td>\n",
       "                        <td id=\"T_3acc1_row0_col6\" class=\"data row0 col6\" >0.4060</td>\n",
       "                        <td id=\"T_3acc1_row0_col7\" class=\"data row0 col7\" >-0.0059</td>\n",
       "                        <td id=\"T_3acc1_row0_col8\" class=\"data row0 col8\" >-0.0057</td>\n",
       "                        <td id=\"T_3acc1_row0_col9\" class=\"data row0 col9\" >-0.0020</td>\n",
       "                        <td id=\"T_3acc1_row0_col10\" class=\"data row0 col10\" >-0.0113</td>\n",
       "                        <td id=\"T_3acc1_row0_col11\" class=\"data row0 col11\" >-0.0428</td>\n",
       "                        <td id=\"T_3acc1_row0_col12\" class=\"data row0 col12\" >-0.0662</td>\n",
       "                        <td id=\"T_3acc1_row0_col13\" class=\"data row0 col13\" >-0.0420</td>\n",
       "                        <td id=\"T_3acc1_row0_col14\" class=\"data row0 col14\" >-0.0141</td>\n",
       "                        <td id=\"T_3acc1_row0_col15\" class=\"data row0 col15\" >-0.0106</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_3acc1_level0_row1\" class=\"row_heading level0 row1\" >CMCSA</th>\n",
       "                        <td id=\"T_3acc1_row1_col0\" class=\"data row1 col0\" >0.2775</td>\n",
       "                        <td id=\"T_3acc1_row1_col1\" class=\"data row1 col1\" >1.0000</td>\n",
       "                        <td id=\"T_3acc1_row1_col2\" class=\"data row1 col2\" >0.2973</td>\n",
       "                        <td id=\"T_3acc1_row1_col3\" class=\"data row1 col3\" >0.2571</td>\n",
       "                        <td id=\"T_3acc1_row1_col4\" class=\"data row1 col4\" >0.2188</td>\n",
       "                        <td id=\"T_3acc1_row1_col5\" class=\"data row1 col5\" >0.3106</td>\n",
       "                        <td id=\"T_3acc1_row1_col6\" class=\"data row1 col6\" >0.3600</td>\n",
       "                        <td id=\"T_3acc1_row1_col7\" class=\"data row1 col7\" >-0.0095</td>\n",
       "                        <td id=\"T_3acc1_row1_col8\" class=\"data row1 col8\" >-0.0225</td>\n",
       "                        <td id=\"T_3acc1_row1_col9\" class=\"data row1 col9\" >-0.0377</td>\n",
       "                        <td id=\"T_3acc1_row1_col10\" class=\"data row1 col10\" >-0.0529</td>\n",
       "                        <td id=\"T_3acc1_row1_col11\" class=\"data row1 col11\" >-0.0456</td>\n",
       "                        <td id=\"T_3acc1_row1_col12\" class=\"data row1 col12\" >-0.0376</td>\n",
       "                        <td id=\"T_3acc1_row1_col13\" class=\"data row1 col13\" >-0.0455</td>\n",
       "                        <td id=\"T_3acc1_row1_col14\" class=\"data row1 col14\" >-0.0314</td>\n",
       "                        <td id=\"T_3acc1_row1_col15\" class=\"data row1 col15\" >-0.0162</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_3acc1_level0_row2\" class=\"row_heading level0 row2\" >CNP</th>\n",
       "                        <td id=\"T_3acc1_row2_col0\" class=\"data row2 col0\" >0.3307</td>\n",
       "                        <td id=\"T_3acc1_row2_col1\" class=\"data row2 col1\" >0.2973</td>\n",
       "                        <td id=\"T_3acc1_row2_col2\" class=\"data row2 col2\" >1.0000</td>\n",
       "                        <td id=\"T_3acc1_row2_col3\" class=\"data row2 col3\" >0.2581</td>\n",
       "                        <td id=\"T_3acc1_row2_col4\" class=\"data row2 col4\" >0.3473</td>\n",
       "                        <td id=\"T_3acc1_row2_col5\" class=\"data row2 col5\" >0.3289</td>\n",
       "                        <td id=\"T_3acc1_row2_col6\" class=\"data row2 col6\" >0.1952</td>\n",
       "                        <td id=\"T_3acc1_row2_col7\" class=\"data row2 col7\" >-0.0126</td>\n",
       "                        <td id=\"T_3acc1_row2_col8\" class=\"data row2 col8\" >-0.0070</td>\n",
       "                        <td id=\"T_3acc1_row2_col9\" class=\"data row2 col9\" >-0.0040</td>\n",
       "                        <td id=\"T_3acc1_row2_col10\" class=\"data row2 col10\" >-0.0200</td>\n",
       "                        <td id=\"T_3acc1_row2_col11\" class=\"data row2 col11\" >-0.0571</td>\n",
       "                        <td id=\"T_3acc1_row2_col12\" class=\"data row2 col12\" >-0.0866</td>\n",
       "                        <td id=\"T_3acc1_row2_col13\" class=\"data row2 col13\" >-0.0768</td>\n",
       "                        <td id=\"T_3acc1_row2_col14\" class=\"data row2 col14\" >-0.0530</td>\n",
       "                        <td id=\"T_3acc1_row2_col15\" class=\"data row2 col15\" >-0.0503</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_3acc1_level0_row3\" class=\"row_heading level0 row3\" >HPQ</th>\n",
       "                        <td id=\"T_3acc1_row3_col0\" class=\"data row3 col0\" >0.3190</td>\n",
       "                        <td id=\"T_3acc1_row3_col1\" class=\"data row3 col1\" >0.2571</td>\n",
       "                        <td id=\"T_3acc1_row3_col2\" class=\"data row3 col2\" >0.2581</td>\n",
       "                        <td id=\"T_3acc1_row3_col3\" class=\"data row3 col3\" >1.0000</td>\n",
       "                        <td id=\"T_3acc1_row3_col4\" class=\"data row3 col4\" >0.1820</td>\n",
       "                        <td id=\"T_3acc1_row3_col5\" class=\"data row3 col5\" >0.3341</td>\n",
       "                        <td id=\"T_3acc1_row3_col6\" class=\"data row3 col6\" >0.3727</td>\n",
       "                        <td id=\"T_3acc1_row3_col7\" class=\"data row3 col7\" >-0.0395</td>\n",
       "                        <td id=\"T_3acc1_row3_col8\" class=\"data row3 col8\" >-0.0428</td>\n",
       "                        <td id=\"T_3acc1_row3_col9\" class=\"data row3 col9\" >-0.0408</td>\n",
       "                        <td id=\"T_3acc1_row3_col10\" class=\"data row3 col10\" >-0.0364</td>\n",
       "                        <td id=\"T_3acc1_row3_col11\" class=\"data row3 col11\" >-0.0454</td>\n",
       "                        <td id=\"T_3acc1_row3_col12\" class=\"data row3 col12\" >-0.0829</td>\n",
       "                        <td id=\"T_3acc1_row3_col13\" class=\"data row3 col13\" >-0.0792</td>\n",
       "                        <td id=\"T_3acc1_row3_col14\" class=\"data row3 col14\" >-0.0582</td>\n",
       "                        <td id=\"T_3acc1_row3_col15\" class=\"data row3 col15\" >-0.0553</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_3acc1_level0_row4\" class=\"row_heading level0 row4\" >PSA</th>\n",
       "                        <td id=\"T_3acc1_row4_col0\" class=\"data row4 col0\" >0.0872</td>\n",
       "                        <td id=\"T_3acc1_row4_col1\" class=\"data row4 col1\" >0.2188</td>\n",
       "                        <td id=\"T_3acc1_row4_col2\" class=\"data row4 col2\" >0.3473</td>\n",
       "                        <td id=\"T_3acc1_row4_col3\" class=\"data row4 col3\" >0.1820</td>\n",
       "                        <td id=\"T_3acc1_row4_col4\" class=\"data row4 col4\" >1.0000</td>\n",
       "                        <td id=\"T_3acc1_row4_col5\" class=\"data row4 col5\" >0.2633</td>\n",
       "                        <td id=\"T_3acc1_row4_col6\" class=\"data row4 col6\" >0.0666</td>\n",
       "                        <td id=\"T_3acc1_row4_col7\" class=\"data row4 col7\" >-0.0067</td>\n",
       "                        <td id=\"T_3acc1_row4_col8\" class=\"data row4 col8\" >0.0057</td>\n",
       "                        <td id=\"T_3acc1_row4_col9\" class=\"data row4 col9\" >0.0096</td>\n",
       "                        <td id=\"T_3acc1_row4_col10\" class=\"data row4 col10\" >0.0034</td>\n",
       "                        <td id=\"T_3acc1_row4_col11\" class=\"data row4 col11\" >-0.0105</td>\n",
       "                        <td id=\"T_3acc1_row4_col12\" class=\"data row4 col12\" >-0.0414</td>\n",
       "                        <td id=\"T_3acc1_row4_col13\" class=\"data row4 col13\" >-0.0674</td>\n",
       "                        <td id=\"T_3acc1_row4_col14\" class=\"data row4 col14\" >-0.0659</td>\n",
       "                        <td id=\"T_3acc1_row4_col15\" class=\"data row4 col15\" >-0.0602</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_3acc1_level0_row5\" class=\"row_heading level0 row5\" >SEE</th>\n",
       "                        <td id=\"T_3acc1_row5_col0\" class=\"data row5 col0\" >0.2098</td>\n",
       "                        <td id=\"T_3acc1_row5_col1\" class=\"data row5 col1\" >0.3106</td>\n",
       "                        <td id=\"T_3acc1_row5_col2\" class=\"data row5 col2\" >0.3289</td>\n",
       "                        <td id=\"T_3acc1_row5_col3\" class=\"data row5 col3\" >0.3341</td>\n",
       "                        <td id=\"T_3acc1_row5_col4\" class=\"data row5 col4\" >0.2633</td>\n",
       "                        <td id=\"T_3acc1_row5_col5\" class=\"data row5 col5\" >1.0000</td>\n",
       "                        <td id=\"T_3acc1_row5_col6\" class=\"data row5 col6\" >0.3617</td>\n",
       "                        <td id=\"T_3acc1_row5_col7\" class=\"data row5 col7\" >-0.0183</td>\n",
       "                        <td id=\"T_3acc1_row5_col8\" class=\"data row5 col8\" >-0.0339</td>\n",
       "                        <td id=\"T_3acc1_row5_col9\" class=\"data row5 col9\" >-0.0396</td>\n",
       "                        <td id=\"T_3acc1_row5_col10\" class=\"data row5 col10\" >-0.0267</td>\n",
       "                        <td id=\"T_3acc1_row5_col11\" class=\"data row5 col11\" >0.0075</td>\n",
       "                        <td id=\"T_3acc1_row5_col12\" class=\"data row5 col12\" >0.0167</td>\n",
       "                        <td id=\"T_3acc1_row5_col13\" class=\"data row5 col13\" >-0.0176</td>\n",
       "                        <td id=\"T_3acc1_row5_col14\" class=\"data row5 col14\" >-0.0414</td>\n",
       "                        <td id=\"T_3acc1_row5_col15\" class=\"data row5 col15\" >-0.0476</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_3acc1_level0_row6\" class=\"row_heading level0 row6\" >ZION</th>\n",
       "                        <td id=\"T_3acc1_row6_col0\" class=\"data row6 col0\" >0.4060</td>\n",
       "                        <td id=\"T_3acc1_row6_col1\" class=\"data row6 col1\" >0.3600</td>\n",
       "                        <td id=\"T_3acc1_row6_col2\" class=\"data row6 col2\" >0.1952</td>\n",
       "                        <td id=\"T_3acc1_row6_col3\" class=\"data row6 col3\" >0.3727</td>\n",
       "                        <td id=\"T_3acc1_row6_col4\" class=\"data row6 col4\" >0.0666</td>\n",
       "                        <td id=\"T_3acc1_row6_col5\" class=\"data row6 col5\" >0.3617</td>\n",
       "                        <td id=\"T_3acc1_row6_col6\" class=\"data row6 col6\" >1.0000</td>\n",
       "                        <td id=\"T_3acc1_row6_col7\" class=\"data row6 col7\" >-0.0185</td>\n",
       "                        <td id=\"T_3acc1_row6_col8\" class=\"data row6 col8\" >-0.0267</td>\n",
       "                        <td id=\"T_3acc1_row6_col9\" class=\"data row6 col9\" >-0.0228</td>\n",
       "                        <td id=\"T_3acc1_row6_col10\" class=\"data row6 col10\" >-0.0148</td>\n",
       "                        <td id=\"T_3acc1_row6_col11\" class=\"data row6 col11\" >-0.0073</td>\n",
       "                        <td id=\"T_3acc1_row6_col12\" class=\"data row6 col12\" >0.0049</td>\n",
       "                        <td id=\"T_3acc1_row6_col13\" class=\"data row6 col13\" >0.0199</td>\n",
       "                        <td id=\"T_3acc1_row6_col14\" class=\"data row6 col14\" >0.0274</td>\n",
       "                        <td id=\"T_3acc1_row6_col15\" class=\"data row6 col15\" >0.0215</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_3acc1_level0_row7\" class=\"row_heading level0 row7\" >R 0</th>\n",
       "                        <td id=\"T_3acc1_row7_col0\" class=\"data row7 col0\" >-0.0059</td>\n",
       "                        <td id=\"T_3acc1_row7_col1\" class=\"data row7 col1\" >-0.0095</td>\n",
       "                        <td id=\"T_3acc1_row7_col2\" class=\"data row7 col2\" >-0.0126</td>\n",
       "                        <td id=\"T_3acc1_row7_col3\" class=\"data row7 col3\" >-0.0395</td>\n",
       "                        <td id=\"T_3acc1_row7_col4\" class=\"data row7 col4\" >-0.0067</td>\n",
       "                        <td id=\"T_3acc1_row7_col5\" class=\"data row7 col5\" >-0.0183</td>\n",
       "                        <td id=\"T_3acc1_row7_col6\" class=\"data row7 col6\" >-0.0185</td>\n",
       "                        <td id=\"T_3acc1_row7_col7\" class=\"data row7 col7\" >1.0000</td>\n",
       "                        <td id=\"T_3acc1_row7_col8\" class=\"data row7 col8\" >0.9233</td>\n",
       "                        <td id=\"T_3acc1_row7_col9\" class=\"data row7 col9\" >0.7183</td>\n",
       "                        <td id=\"T_3acc1_row7_col10\" class=\"data row7 col10\" >0.2411</td>\n",
       "                        <td id=\"T_3acc1_row7_col11\" class=\"data row7 col11\" >-0.0500</td>\n",
       "                        <td id=\"T_3acc1_row7_col12\" class=\"data row7 col12\" >0.0302</td>\n",
       "                        <td id=\"T_3acc1_row7_col13\" class=\"data row7 col13\" >0.0256</td>\n",
       "                        <td id=\"T_3acc1_row7_col14\" class=\"data row7 col14\" >0.0284</td>\n",
       "                        <td id=\"T_3acc1_row7_col15\" class=\"data row7 col15\" >0.0489</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_3acc1_level0_row8\" class=\"row_heading level0 row8\" >R 90</th>\n",
       "                        <td id=\"T_3acc1_row8_col0\" class=\"data row8 col0\" >-0.0057</td>\n",
       "                        <td id=\"T_3acc1_row8_col1\" class=\"data row8 col1\" >-0.0225</td>\n",
       "                        <td id=\"T_3acc1_row8_col2\" class=\"data row8 col2\" >-0.0070</td>\n",
       "                        <td id=\"T_3acc1_row8_col3\" class=\"data row8 col3\" >-0.0428</td>\n",
       "                        <td id=\"T_3acc1_row8_col4\" class=\"data row8 col4\" >0.0057</td>\n",
       "                        <td id=\"T_3acc1_row8_col5\" class=\"data row8 col5\" >-0.0339</td>\n",
       "                        <td id=\"T_3acc1_row8_col6\" class=\"data row8 col6\" >-0.0267</td>\n",
       "                        <td id=\"T_3acc1_row8_col7\" class=\"data row8 col7\" >0.9233</td>\n",
       "                        <td id=\"T_3acc1_row8_col8\" class=\"data row8 col8\" >1.0000</td>\n",
       "                        <td id=\"T_3acc1_row8_col9\" class=\"data row8 col9\" >0.9197</td>\n",
       "                        <td id=\"T_3acc1_row8_col10\" class=\"data row8 col10\" >0.5137</td>\n",
       "                        <td id=\"T_3acc1_row8_col11\" class=\"data row8 col11\" >0.0981</td>\n",
       "                        <td id=\"T_3acc1_row8_col12\" class=\"data row8 col12\" >0.0442</td>\n",
       "                        <td id=\"T_3acc1_row8_col13\" class=\"data row8 col13\" >0.0441</td>\n",
       "                        <td id=\"T_3acc1_row8_col14\" class=\"data row8 col14\" >0.0441</td>\n",
       "                        <td id=\"T_3acc1_row8_col15\" class=\"data row8 col15\" >0.0523</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_3acc1_level0_row9\" class=\"row_heading level0 row9\" >R 180</th>\n",
       "                        <td id=\"T_3acc1_row9_col0\" class=\"data row9 col0\" >-0.0020</td>\n",
       "                        <td id=\"T_3acc1_row9_col1\" class=\"data row9 col1\" >-0.0377</td>\n",
       "                        <td id=\"T_3acc1_row9_col2\" class=\"data row9 col2\" >-0.0040</td>\n",
       "                        <td id=\"T_3acc1_row9_col3\" class=\"data row9 col3\" >-0.0408</td>\n",
       "                        <td id=\"T_3acc1_row9_col4\" class=\"data row9 col4\" >0.0096</td>\n",
       "                        <td id=\"T_3acc1_row9_col5\" class=\"data row9 col5\" >-0.0396</td>\n",
       "                        <td id=\"T_3acc1_row9_col6\" class=\"data row9 col6\" >-0.0228</td>\n",
       "                        <td id=\"T_3acc1_row9_col7\" class=\"data row9 col7\" >0.7183</td>\n",
       "                        <td id=\"T_3acc1_row9_col8\" class=\"data row9 col8\" >0.9197</td>\n",
       "                        <td id=\"T_3acc1_row9_col9\" class=\"data row9 col9\" >1.0000</td>\n",
       "                        <td id=\"T_3acc1_row9_col10\" class=\"data row9 col10\" >0.7901</td>\n",
       "                        <td id=\"T_3acc1_row9_col11\" class=\"data row9 col11\" >0.3509</td>\n",
       "                        <td id=\"T_3acc1_row9_col12\" class=\"data row9 col12\" >0.1002</td>\n",
       "                        <td id=\"T_3acc1_row9_col13\" class=\"data row9 col13\" >0.0579</td>\n",
       "                        <td id=\"T_3acc1_row9_col14\" class=\"data row9 col14\" >0.0700</td>\n",
       "                        <td id=\"T_3acc1_row9_col15\" class=\"data row9 col15\" >0.0732</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_3acc1_level0_row10\" class=\"row_heading level0 row10\" >R 360</th>\n",
       "                        <td id=\"T_3acc1_row10_col0\" class=\"data row10 col0\" >-0.0113</td>\n",
       "                        <td id=\"T_3acc1_row10_col1\" class=\"data row10 col1\" >-0.0529</td>\n",
       "                        <td id=\"T_3acc1_row10_col2\" class=\"data row10 col2\" >-0.0200</td>\n",
       "                        <td id=\"T_3acc1_row10_col3\" class=\"data row10 col3\" >-0.0364</td>\n",
       "                        <td id=\"T_3acc1_row10_col4\" class=\"data row10 col4\" >0.0034</td>\n",
       "                        <td id=\"T_3acc1_row10_col5\" class=\"data row10 col5\" >-0.0267</td>\n",
       "                        <td id=\"T_3acc1_row10_col6\" class=\"data row10 col6\" >-0.0148</td>\n",
       "                        <td id=\"T_3acc1_row10_col7\" class=\"data row10 col7\" >0.2411</td>\n",
       "                        <td id=\"T_3acc1_row10_col8\" class=\"data row10 col8\" >0.5137</td>\n",
       "                        <td id=\"T_3acc1_row10_col9\" class=\"data row10 col9\" >0.7901</td>\n",
       "                        <td id=\"T_3acc1_row10_col10\" class=\"data row10 col10\" >1.0000</td>\n",
       "                        <td id=\"T_3acc1_row10_col11\" class=\"data row10 col11\" >0.7887</td>\n",
       "                        <td id=\"T_3acc1_row10_col12\" class=\"data row10 col12\" >0.2784</td>\n",
       "                        <td id=\"T_3acc1_row10_col13\" class=\"data row10 col13\" >0.0751</td>\n",
       "                        <td id=\"T_3acc1_row10_col14\" class=\"data row10 col14\" >0.1126</td>\n",
       "                        <td id=\"T_3acc1_row10_col15\" class=\"data row10 col15\" >0.1252</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_3acc1_level0_row11\" class=\"row_heading level0 row11\" >R 720</th>\n",
       "                        <td id=\"T_3acc1_row11_col0\" class=\"data row11 col0\" >-0.0428</td>\n",
       "                        <td id=\"T_3acc1_row11_col1\" class=\"data row11 col1\" >-0.0456</td>\n",
       "                        <td id=\"T_3acc1_row11_col2\" class=\"data row11 col2\" >-0.0571</td>\n",
       "                        <td id=\"T_3acc1_row11_col3\" class=\"data row11 col3\" >-0.0454</td>\n",
       "                        <td id=\"T_3acc1_row11_col4\" class=\"data row11 col4\" >-0.0105</td>\n",
       "                        <td id=\"T_3acc1_row11_col5\" class=\"data row11 col5\" >0.0075</td>\n",
       "                        <td id=\"T_3acc1_row11_col6\" class=\"data row11 col6\" >-0.0073</td>\n",
       "                        <td id=\"T_3acc1_row11_col7\" class=\"data row11 col7\" >-0.0500</td>\n",
       "                        <td id=\"T_3acc1_row11_col8\" class=\"data row11 col8\" >0.0981</td>\n",
       "                        <td id=\"T_3acc1_row11_col9\" class=\"data row11 col9\" >0.3509</td>\n",
       "                        <td id=\"T_3acc1_row11_col10\" class=\"data row11 col10\" >0.7887</td>\n",
       "                        <td id=\"T_3acc1_row11_col11\" class=\"data row11 col11\" >1.0000</td>\n",
       "                        <td id=\"T_3acc1_row11_col12\" class=\"data row11 col12\" >0.6022</td>\n",
       "                        <td id=\"T_3acc1_row11_col13\" class=\"data row11 col13\" >0.1808</td>\n",
       "                        <td id=\"T_3acc1_row11_col14\" class=\"data row11 col14\" >0.1729</td>\n",
       "                        <td id=\"T_3acc1_row11_col15\" class=\"data row11 col15\" >0.1952</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_3acc1_level0_row12\" class=\"row_heading level0 row12\" >R 1800</th>\n",
       "                        <td id=\"T_3acc1_row12_col0\" class=\"data row12 col0\" >-0.0662</td>\n",
       "                        <td id=\"T_3acc1_row12_col1\" class=\"data row12 col1\" >-0.0376</td>\n",
       "                        <td id=\"T_3acc1_row12_col2\" class=\"data row12 col2\" >-0.0866</td>\n",
       "                        <td id=\"T_3acc1_row12_col3\" class=\"data row12 col3\" >-0.0829</td>\n",
       "                        <td id=\"T_3acc1_row12_col4\" class=\"data row12 col4\" >-0.0414</td>\n",
       "                        <td id=\"T_3acc1_row12_col5\" class=\"data row12 col5\" >0.0167</td>\n",
       "                        <td id=\"T_3acc1_row12_col6\" class=\"data row12 col6\" >0.0049</td>\n",
       "                        <td id=\"T_3acc1_row12_col7\" class=\"data row12 col7\" >0.0302</td>\n",
       "                        <td id=\"T_3acc1_row12_col8\" class=\"data row12 col8\" >0.0442</td>\n",
       "                        <td id=\"T_3acc1_row12_col9\" class=\"data row12 col9\" >0.1002</td>\n",
       "                        <td id=\"T_3acc1_row12_col10\" class=\"data row12 col10\" >0.2784</td>\n",
       "                        <td id=\"T_3acc1_row12_col11\" class=\"data row12 col11\" >0.6022</td>\n",
       "                        <td id=\"T_3acc1_row12_col12\" class=\"data row12 col12\" >1.0000</td>\n",
       "                        <td id=\"T_3acc1_row12_col13\" class=\"data row12 col13\" >0.7349</td>\n",
       "                        <td id=\"T_3acc1_row12_col14\" class=\"data row12 col14\" >0.4609</td>\n",
       "                        <td id=\"T_3acc1_row12_col15\" class=\"data row12 col15\" >0.3664</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_3acc1_level0_row13\" class=\"row_heading level0 row13\" >R 3600</th>\n",
       "                        <td id=\"T_3acc1_row13_col0\" class=\"data row13 col0\" >-0.0420</td>\n",
       "                        <td id=\"T_3acc1_row13_col1\" class=\"data row13 col1\" >-0.0455</td>\n",
       "                        <td id=\"T_3acc1_row13_col2\" class=\"data row13 col2\" >-0.0768</td>\n",
       "                        <td id=\"T_3acc1_row13_col3\" class=\"data row13 col3\" >-0.0792</td>\n",
       "                        <td id=\"T_3acc1_row13_col4\" class=\"data row13 col4\" >-0.0674</td>\n",
       "                        <td id=\"T_3acc1_row13_col5\" class=\"data row13 col5\" >-0.0176</td>\n",
       "                        <td id=\"T_3acc1_row13_col6\" class=\"data row13 col6\" >0.0199</td>\n",
       "                        <td id=\"T_3acc1_row13_col7\" class=\"data row13 col7\" >0.0256</td>\n",
       "                        <td id=\"T_3acc1_row13_col8\" class=\"data row13 col8\" >0.0441</td>\n",
       "                        <td id=\"T_3acc1_row13_col9\" class=\"data row13 col9\" >0.0579</td>\n",
       "                        <td id=\"T_3acc1_row13_col10\" class=\"data row13 col10\" >0.0751</td>\n",
       "                        <td id=\"T_3acc1_row13_col11\" class=\"data row13 col11\" >0.1808</td>\n",
       "                        <td id=\"T_3acc1_row13_col12\" class=\"data row13 col12\" >0.7349</td>\n",
       "                        <td id=\"T_3acc1_row13_col13\" class=\"data row13 col13\" >1.0000</td>\n",
       "                        <td id=\"T_3acc1_row13_col14\" class=\"data row13 col14\" >0.8102</td>\n",
       "                        <td id=\"T_3acc1_row13_col15\" class=\"data row13 col15\" >0.6100</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_3acc1_level0_row14\" class=\"row_heading level0 row14\" >R 7200</th>\n",
       "                        <td id=\"T_3acc1_row14_col0\" class=\"data row14 col0\" >-0.0141</td>\n",
       "                        <td id=\"T_3acc1_row14_col1\" class=\"data row14 col1\" >-0.0314</td>\n",
       "                        <td id=\"T_3acc1_row14_col2\" class=\"data row14 col2\" >-0.0530</td>\n",
       "                        <td id=\"T_3acc1_row14_col3\" class=\"data row14 col3\" >-0.0582</td>\n",
       "                        <td id=\"T_3acc1_row14_col4\" class=\"data row14 col4\" >-0.0659</td>\n",
       "                        <td id=\"T_3acc1_row14_col5\" class=\"data row14 col5\" >-0.0414</td>\n",
       "                        <td id=\"T_3acc1_row14_col6\" class=\"data row14 col6\" >0.0274</td>\n",
       "                        <td id=\"T_3acc1_row14_col7\" class=\"data row14 col7\" >0.0284</td>\n",
       "                        <td id=\"T_3acc1_row14_col8\" class=\"data row14 col8\" >0.0441</td>\n",
       "                        <td id=\"T_3acc1_row14_col9\" class=\"data row14 col9\" >0.0700</td>\n",
       "                        <td id=\"T_3acc1_row14_col10\" class=\"data row14 col10\" >0.1126</td>\n",
       "                        <td id=\"T_3acc1_row14_col11\" class=\"data row14 col11\" >0.1729</td>\n",
       "                        <td id=\"T_3acc1_row14_col12\" class=\"data row14 col12\" >0.4609</td>\n",
       "                        <td id=\"T_3acc1_row14_col13\" class=\"data row14 col13\" >0.8102</td>\n",
       "                        <td id=\"T_3acc1_row14_col14\" class=\"data row14 col14\" >1.0000</td>\n",
       "                        <td id=\"T_3acc1_row14_col15\" class=\"data row14 col15\" >0.9189</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_3acc1_level0_row15\" class=\"row_heading level0 row15\" >R 10800</th>\n",
       "                        <td id=\"T_3acc1_row15_col0\" class=\"data row15 col0\" >-0.0106</td>\n",
       "                        <td id=\"T_3acc1_row15_col1\" class=\"data row15 col1\" >-0.0162</td>\n",
       "                        <td id=\"T_3acc1_row15_col2\" class=\"data row15 col2\" >-0.0503</td>\n",
       "                        <td id=\"T_3acc1_row15_col3\" class=\"data row15 col3\" >-0.0553</td>\n",
       "                        <td id=\"T_3acc1_row15_col4\" class=\"data row15 col4\" >-0.0602</td>\n",
       "                        <td id=\"T_3acc1_row15_col5\" class=\"data row15 col5\" >-0.0476</td>\n",
       "                        <td id=\"T_3acc1_row15_col6\" class=\"data row15 col6\" >0.0215</td>\n",
       "                        <td id=\"T_3acc1_row15_col7\" class=\"data row15 col7\" >0.0489</td>\n",
       "                        <td id=\"T_3acc1_row15_col8\" class=\"data row15 col8\" >0.0523</td>\n",
       "                        <td id=\"T_3acc1_row15_col9\" class=\"data row15 col9\" >0.0732</td>\n",
       "                        <td id=\"T_3acc1_row15_col10\" class=\"data row15 col10\" >0.1252</td>\n",
       "                        <td id=\"T_3acc1_row15_col11\" class=\"data row15 col11\" >0.1952</td>\n",
       "                        <td id=\"T_3acc1_row15_col12\" class=\"data row15 col12\" >0.3664</td>\n",
       "                        <td id=\"T_3acc1_row15_col13\" class=\"data row15 col13\" >0.6100</td>\n",
       "                        <td id=\"T_3acc1_row15_col14\" class=\"data row15 col14\" >0.9189</td>\n",
       "                        <td id=\"T_3acc1_row15_col15\" class=\"data row15 col15\" >1.0000</td>\n",
       "            </tr>\n",
       "    </tbody></table>"
      ],
      "text/plain": [
       "<pandas.io.formats.style.Styler at 0x7ff2807ebc10>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "########################################################################\n",
    "# Displaying factors statistics\n",
    "########################################################################\n",
    "\n",
    "table = pd.concat([loadings.min(), loadings.max()], axis=1)\n",
    "table.columns = ['min', 'max']\n",
    "display(table.iloc[:16,:].style.format(\"{:.4f}\").background_gradient(cmap='YlGn'))\n",
    "display(X.iloc[:,:16].corr().style.format(\"{:.4f}\").background_gradient(cmap='YlGn'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.2 Creating Constraints on Key Rate Durations\n",
    "\n",
    "In this example we are going to put a limit on the maximum duration that the portfolio can reach. The key rate durations of portfolio for 1800, 3600 and 7200 days will be lower than -2, -2 and -3."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Disabled</th>\n",
       "      <th>Factor</th>\n",
       "      <th>Sign</th>\n",
       "      <th>Value</th>\n",
       "      <th>Relative Factor</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>False</td>\n",
       "      <td>R 1800</td>\n",
       "      <td>&lt;=</td>\n",
       "      <td>-2</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>False</td>\n",
       "      <td>R 3600</td>\n",
       "      <td>&lt;=</td>\n",
       "      <td>-2</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>False</td>\n",
       "      <td>R 7200</td>\n",
       "      <td>&lt;=</td>\n",
       "      <td>-3</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Disabled  Factor Sign  Value Relative Factor\n",
       "0     False  R 1800   <=     -2                \n",
       "1     False  R 3600   <=     -2                \n",
       "2     False  R 7200   <=     -3                "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "########################################################################\n",
    "# Creating key rate durations constraints\n",
    "########################################################################\n",
    "\n",
    "constraints = {'Disabled': [False, False, False],\n",
    "               'Factor': ['R 1800', 'R 3600', 'R 7200'],\n",
    "               'Sign': ['<=', '<=', '<='],\n",
    "               'Value': [-2, -2, -3],\n",
    "               'Relative Factor': ['', '', '']}\n",
    "\n",
    "constraints = pd.DataFrame(constraints)\n",
    "\n",
    "display(constraints)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.3 Estimating Optimum Portfolio with Key Rate Durations Constraints"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style  type=\"text/css\" >\n",
       "#T_6d22a_row0_col0,#T_6d22a_row1_col0,#T_6d22a_row2_col0,#T_6d22a_row4_col0,#T_6d22a_row5_col0,#T_6d22a_row6_col0,#T_6d22a_row7_col0,#T_6d22a_row8_col0,#T_6d22a_row9_col0{\n",
       "            background-color:  #ffffe5;\n",
       "            color:  #000000;\n",
       "        }#T_6d22a_row3_col0{\n",
       "            background-color:  #f3fab6;\n",
       "            color:  #000000;\n",
       "        }#T_6d22a_row10_col0{\n",
       "            background-color:  #248644;\n",
       "            color:  #000000;\n",
       "        }#T_6d22a_row11_col0{\n",
       "            background-color:  #61bb6d;\n",
       "            color:  #000000;\n",
       "        }#T_6d22a_row12_col0{\n",
       "            background-color:  #004529;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_6d22a_row13_col0{\n",
       "            background-color:  #dff3a8;\n",
       "            color:  #000000;\n",
       "        }</style><table id=\"T_6d22a_\" ><thead>    <tr>        <th class=\"blank level0\" ></th>        <th class=\"col_heading level0 col0\" >weights</th>    </tr></thead><tbody>\n",
       "                <tr>\n",
       "                        <th id=\"T_6d22a_level0_row0\" class=\"row_heading level0 row0\" >APA</th>\n",
       "                        <td id=\"T_6d22a_row0_col0\" class=\"data row0 col0\" >0.0000%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_6d22a_level0_row1\" class=\"row_heading level0 row1\" >CMCSA</th>\n",
       "                        <td id=\"T_6d22a_row1_col0\" class=\"data row1 col0\" >0.0000%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_6d22a_level0_row2\" class=\"row_heading level0 row2\" >CNP</th>\n",
       "                        <td id=\"T_6d22a_row2_col0\" class=\"data row2 col0\" >0.0000%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_6d22a_level0_row3\" class=\"row_heading level0 row3\" >HPQ</th>\n",
       "                        <td id=\"T_6d22a_row3_col0\" class=\"data row3 col0\" >5.3864%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_6d22a_level0_row4\" class=\"row_heading level0 row4\" >PSA</th>\n",
       "                        <td id=\"T_6d22a_row4_col0\" class=\"data row4 col0\" >0.0000%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_6d22a_level0_row5\" class=\"row_heading level0 row5\" >SEE</th>\n",
       "                        <td id=\"T_6d22a_row5_col0\" class=\"data row5 col0\" >0.0000%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_6d22a_level0_row6\" class=\"row_heading level0 row6\" >ZION</th>\n",
       "                        <td id=\"T_6d22a_row6_col0\" class=\"data row6 col0\" >0.0000%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_6d22a_level0_row7\" class=\"row_heading level0 row7\" >PEP11900D031</th>\n",
       "                        <td id=\"T_6d22a_row7_col0\" class=\"data row7 col0\" >0.0000%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_6d22a_level0_row8\" class=\"row_heading level0 row8\" >PEP13000D012</th>\n",
       "                        <td id=\"T_6d22a_row8_col0\" class=\"data row8 col0\" >0.0000%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_6d22a_level0_row9\" class=\"row_heading level0 row9\" >PEP13000M088</th>\n",
       "                        <td id=\"T_6d22a_row9_col0\" class=\"data row9 col0\" >0.0000%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_6d22a_level0_row10\" class=\"row_heading level0 row10\" >PEP23900M103</th>\n",
       "                        <td id=\"T_6d22a_row10_col0\" class=\"data row10 col0\" >27.9269%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_6d22a_level0_row11\" class=\"row_heading level0 row11\" >PEP70101M530</th>\n",
       "                        <td id=\"T_6d22a_row11_col0\" class=\"data row11 col0\" >20.7125%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_6d22a_level0_row12\" class=\"row_heading level0 row12\" >PEP70101M571</th>\n",
       "                        <td id=\"T_6d22a_row12_col0\" class=\"data row12 col0\" >37.5375%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_6d22a_level0_row13\" class=\"row_heading level0 row13\" >PEP70310M156</th>\n",
       "                        <td id=\"T_6d22a_row13_col0\" class=\"data row13 col0\" >8.4368%</td>\n",
       "            </tr>\n",
       "    </tbody></table>"
      ],
      "text/plain": [
       "<pandas.io.formats.style.Styler at 0x7ff2850c8460>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "########################################################################\n",
    "# Estimating optimum portfolio with key rate durations constraints\n",
    "########################################################################\n",
    "\n",
    "C, D = cf.factors_constraints(constraints, loadings)\n",
    "\n",
    "port.ainequality = C\n",
    "port.binequality = D\n",
    "\n",
    "w = port.optimization(model=model, rm=rm, obj=obj, rf=rf, l=l, hist=hist)\n",
    "\n",
    "display(w.style.format(\"{:.4%}\").background_gradient(cmap='YlGn'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style  type=\"text/css\" >\n",
       "#T_51ad0_row0_col0,#T_51ad0_row1_col0,#T_51ad0_row2_col0,#T_51ad0_row3_col0,#T_51ad0_row4_col0,#T_51ad0_row5_col0,#T_51ad0_row6_col0,#T_51ad0_row7_col0,#T_51ad0_row8_col0,#T_51ad0_row9_col0,#T_51ad0_row16_col0,#T_51ad0_row17_col0,#T_51ad0_row18_col0,#T_51ad0_row19_col0{\n",
       "            background-color:  #f7fcba;\n",
       "            color:  #000000;\n",
       "        }#T_51ad0_row10_col0{\n",
       "            background-color:  #f7fcbc;\n",
       "            color:  #000000;\n",
       "        }#T_51ad0_row11_col0{\n",
       "            background-color:  #f8fcbe;\n",
       "            color:  #000000;\n",
       "        }#T_51ad0_row12_col0,#T_51ad0_row13_col0{\n",
       "            background-color:  #fcfed7;\n",
       "            color:  #000000;\n",
       "        }#T_51ad0_row14_col0{\n",
       "            background-color:  #ffffe5;\n",
       "            color:  #000000;\n",
       "        }#T_51ad0_row15_col0{\n",
       "            background-color:  #f8fcbd;\n",
       "            color:  #000000;\n",
       "        }#T_51ad0_row20_col0{\n",
       "            background-color:  #f5fbb8;\n",
       "            color:  #000000;\n",
       "        }#T_51ad0_row21_col0{\n",
       "            background-color:  #bce395;\n",
       "            color:  #000000;\n",
       "        }#T_51ad0_row22_col0{\n",
       "            background-color:  #92d183;\n",
       "            color:  #000000;\n",
       "        }#T_51ad0_row23_col0{\n",
       "            background-color:  #004529;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_51ad0_row24_col0{\n",
       "            background-color:  #f6fcb8;\n",
       "            color:  #000000;\n",
       "        }</style><table id=\"T_51ad0_\" ><thead>    <tr>        <th class=\"blank level0\" ></th>        <th class=\"col_heading level0 col0\" >Values</th>    </tr></thead><tbody>\n",
       "                <tr>\n",
       "                        <th id=\"T_51ad0_level0_row0\" class=\"row_heading level0 row0\" >APA</th>\n",
       "                        <td id=\"T_51ad0_row0_col0\" class=\"data row0 col0\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_51ad0_level0_row1\" class=\"row_heading level0 row1\" >CMCSA</th>\n",
       "                        <td id=\"T_51ad0_row1_col0\" class=\"data row1 col0\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_51ad0_level0_row2\" class=\"row_heading level0 row2\" >CNP</th>\n",
       "                        <td id=\"T_51ad0_row2_col0\" class=\"data row2 col0\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_51ad0_level0_row3\" class=\"row_heading level0 row3\" >HPQ</th>\n",
       "                        <td id=\"T_51ad0_row3_col0\" class=\"data row3 col0\" >0.0539</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_51ad0_level0_row4\" class=\"row_heading level0 row4\" >PSA</th>\n",
       "                        <td id=\"T_51ad0_row4_col0\" class=\"data row4 col0\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_51ad0_level0_row5\" class=\"row_heading level0 row5\" >SEE</th>\n",
       "                        <td id=\"T_51ad0_row5_col0\" class=\"data row5 col0\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_51ad0_level0_row6\" class=\"row_heading level0 row6\" >ZION</th>\n",
       "                        <td id=\"T_51ad0_row6_col0\" class=\"data row6 col0\" >0.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_51ad0_level0_row7\" class=\"row_heading level0 row7\" >R 0</th>\n",
       "                        <td id=\"T_51ad0_row7_col0\" class=\"data row7 col0\" >-0.0006</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_51ad0_level0_row8\" class=\"row_heading level0 row8\" >R 90</th>\n",
       "                        <td id=\"T_51ad0_row8_col0\" class=\"data row8 col0\" >-0.0030</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_51ad0_level0_row9\" class=\"row_heading level0 row9\" >R 180</th>\n",
       "                        <td id=\"T_51ad0_row9_col0\" class=\"data row9 col0\" >-0.0109</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_51ad0_level0_row10\" class=\"row_heading level0 row10\" >R 360</th>\n",
       "                        <td id=\"T_51ad0_row10_col0\" class=\"data row10 col0\" >-0.0427</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_51ad0_level0_row11\" class=\"row_heading level0 row11\" >R 720</th>\n",
       "                        <td id=\"T_51ad0_row11_col0\" class=\"data row11 col0\" >-0.2433</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_51ad0_level0_row12\" class=\"row_heading level0 row12\" >R 1800</th>\n",
       "                        <td id=\"T_51ad0_row12_col0\" class=\"data row12 col0\" >-2.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_51ad0_level0_row13\" class=\"row_heading level0 row13\" >R 3600</th>\n",
       "                        <td id=\"T_51ad0_row13_col0\" class=\"data row13 col0\" >-2.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_51ad0_level0_row14\" class=\"row_heading level0 row14\" >R 7200</th>\n",
       "                        <td id=\"T_51ad0_row14_col0\" class=\"data row14 col0\" >-3.0000</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_51ad0_level0_row15\" class=\"row_heading level0 row15\" >R 10800</th>\n",
       "                        <td id=\"T_51ad0_row15_col0\" class=\"data row15 col0\" >-0.1690</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_51ad0_level0_row16\" class=\"row_heading level0 row16\" >R^2 0</th>\n",
       "                        <td id=\"T_51ad0_row16_col0\" class=\"data row16 col0\" >0.0001</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_51ad0_level0_row17\" class=\"row_heading level0 row17\" >R^2 90</th>\n",
       "                        <td id=\"T_51ad0_row17_col0\" class=\"data row17 col0\" >0.0011</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_51ad0_level0_row18\" class=\"row_heading level0 row18\" >R^2 180</th>\n",
       "                        <td id=\"T_51ad0_row18_col0\" class=\"data row18 col0\" >0.0054</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_51ad0_level0_row19\" class=\"row_heading level0 row19\" >R^2 360</th>\n",
       "                        <td id=\"T_51ad0_row19_col0\" class=\"data row19 col0\" >0.0290</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_51ad0_level0_row20\" class=\"row_heading level0 row20\" >R^2 720</th>\n",
       "                        <td id=\"T_51ad0_row20_col0\" class=\"data row20 col0\" >0.2677</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_51ad0_level0_row21\" class=\"row_heading level0 row21\" >R^2 1800</th>\n",
       "                        <td id=\"T_51ad0_row21_col0\" class=\"data row21 col0\" >5.1788</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_51ad0_level0_row22\" class=\"row_heading level0 row22\" >R^2 3600</th>\n",
       "                        <td id=\"T_51ad0_row22_col0\" class=\"data row22 col0\" >7.7747</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_51ad0_level0_row23\" class=\"row_heading level0 row23\" >R^2 7200</th>\n",
       "                        <td id=\"T_51ad0_row23_col0\" class=\"data row23 col0\" >21.4398</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_51ad0_level0_row24\" class=\"row_heading level0 row24\" >R^2 10800</th>\n",
       "                        <td id=\"T_51ad0_row24_col0\" class=\"data row24 col0\" >0.1698</td>\n",
       "            </tr>\n",
       "    </tbody></table>"
      ],
      "text/plain": [
       "<pandas.io.formats.style.Styler at 0x7ff2850c8ee0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "########################################################################\n",
    "# Calculating portfolio sensitivities for each risk factor\n",
    "########################################################################\n",
    "\n",
    "d_ = np.matrix(loadings).T * np.matrix(w)\n",
    "d_ = pd.DataFrame(d_, index=loadings.columns, columns=['Values'])\n",
    "\n",
    "display(d_.style.format(\"{:.4f}\").background_gradient(cmap='YlGn'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can see that this portfolio fulfill all constraints on key rate durations."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
